11 Replies - 1751 Views - Last Post: 20 December 2013 - 01:09 AM Rate Topic: -----

#1 Roland_J  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-August 13

Multiple and

Posted 01 November 2013 - 09:51 AM

How does one write multiple and's?
It's fine to write; if a=2 and b=3, but what happens when you have 20 conditions to 'and', with long variable names? No page is long enough. I can't find an example that uses a list of and's.

The only other way I see is to use multiple 'if then', tailed by dozens of 'end if's

Roland
Is This A Good Question/Topic? 0
  • +

Replies To: Multiple and

#2 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,781
  • Joined: 26-March 09

Re: Multiple and

Posted 01 November 2013 - 10:07 AM

You can move them onto a separate lines using an underscore.

if a=1 and b=3 and _
   c=3 then

end if 

Was This Post Helpful? 1
  • +
  • -

#3 Roland_J  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-August 13

Re: Multiple and

Posted 01 November 2013 - 10:16 AM

Ok, I figured it out. If statements allow me to trap the error condition too, like this;

if getvariable("a") = 4 then
 if getvariable("b") = 9 then
  if getvariable("c") =  7 then
   if getvariable("d") =  11 then
    setvariable "AA", "PASS"
     else setvariable "AA", "fail on 11"
     end if
   else setvariable "AA", "fail on 7"
   end if
 else setvariable "AA", "fail on 9"
 end if
else setvariable "AA", "fail on 3"
end if



Regards
Roland
Was This Post Helpful? 0
  • +
  • -

#4 Roland_J  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-August 13

Re: Multiple and

Posted 01 November 2013 - 10:21 AM

Thanks maj3091

I think I'll use the multiple 'if's since I need to know what caused the error, even though it only traps the first error condition.

Roland
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3599
  • View blog
  • Posts: 12,386
  • Joined: 12-December 12

Re: Multiple and

Posted 01 November 2013 - 10:23 AM

I would prefer to write it this way:

if getvariable("a") <> 4 then
    setvariable "AA", "fail on 4"
elseif getvariable("b") <> 9 then
    setvariable "AA", "fail on 9"
elseif getvariable("c") <> 7 then
    setvariable "AA", "fail on 7"
elseif getvariable("c") <> 11 then
    setvariable "AA", "fail on 11"
else
    setvariable "AA", "PASS"
end if

Was This Post Helpful? 0
  • +
  • -

#6 Roland_J  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-August 13

Re: Multiple and

Posted 01 November 2013 - 10:45 AM

Thanks Andrewsw

Gee, that was quick, and much better..

While I'm here, can I ask what the most sensible way to do this would be;

My machine tests 20 parameters on a part, making a 'present' or 'absent' decision on each one.
The same tests get done on every part, but there are 16 variants of the same part. (tested in batches of one variant at a time)

So at the moment I'm doing the test first and getting 20 results, then will do the 'elseif' check that you showed against the selected variant. I can now set variables according to the different fails too.

Thus, I will have 16 clumps of quite similar code to cater for all the variants. Is that sensible? I have a feeling I should be using a large matrix, but I don't see an advantage to it.

Roland
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3599
  • View blog
  • Posts: 12,386
  • Joined: 12-December 12

Re: Multiple and

Posted 01 November 2013 - 12:43 PM

I can't really tell. If you have 16 clumps of similar code then it is very likely that it can be improved, possibly using functions, procedures, or a dictionary or matrix of some kind.
Was This Post Helpful? 0
  • +
  • -

#8 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,781
  • Joined: 26-March 09

Re: Multiple and

Posted 01 November 2013 - 04:15 PM

As Andrew has suggested, you could probably use a function/sub to do this.

If you know the the results you expect, then you could maybe pass them into a function along with the user inputs.

Not sure what you want to do with the fail messages though.

Something like below might work (rough and ready, but should give you an idea of what I was trying to say).

Private function fblnCheckValues (aintInput() as integer, aintResults() as integer) as boolean
dim intLoop as integer
dim blnAllGood as boolean

  blnAllGood = true

  for intLoop = 0 to ubound(aintInput())
    if aintInput(intLoop)<>aintResults(intLoop) then
       ' Write your fails here - maybe another array????

       ' Set flag here to indicate a fail
       blnAllGood = false

    endif
  Next intloop

  'Return result here
  fblnCheckValues = blnAllGood


end sub


Hope that helps a little

This post has been edited by maj3091: 01 November 2013 - 04:15 PM

Was This Post Helpful? 0
  • +
  • -

#9 Roland_J  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-August 13

Re: Multiple and

Posted 19 December 2013 - 02:24 PM

Hi All

Can anyone tell me what's wrong with this code?
In this case, it is 'GS1' and all the conditions are correct, so it should get to the end with a pass, but it faults at the first line, giving 'final result' as 'NO LID SCREW 1'

I don't see how to get through all the checks unless I use the 'elseif' test in this manner.

Help appreciated.
Roland

if GetVariable("variant") = "GS1" then

elseif GetVariable ("T04_LID_SCREW1") <> "PRESENT" then
    setvariable "final_result","NO LID SCREW 1"

elseif GetVariable ("T05_HINGE_SCREW2") <> "PRESENT" then
 setvariable "final_result","NO HINGE SCREW 2"

elseif GetVariable ("T06_HINGE_SCREW3") <> "PRESENT" then
 setvariable "final_result","NO HINGE SCREW 3"

elseif GetVariable ("T07_LID_SCREW6") <> "PRESENT" then
 setvariable "final_result","NO LID SCREW 6"

elseif GetVariable ("T08a_GROMMET") <> "PRESENT" then
 setvariable "final_result","MISSING GROMMET"

 else setvariable "final_result", "RESULT PASS"
end if


View Postandrewsw, on 01 November 2013 - 10:23 AM, said:

I would prefer to write it this way:

if getvariable("a") <> 4 then
    setvariable "AA", "fail on 4"
elseif getvariable("b") <> 9 then
    setvariable "AA", "fail on 9"
elseif getvariable("c") <> 7 then
    setvariable "AA", "fail on 7"
elseif getvariable("c") <> 11 then
    setvariable "AA", "fail on 11"
else
    setvariable "AA", "PASS"
end if

This post has been edited by andrewsw: 19 December 2013 - 02:33 PM

Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3599
  • View blog
  • Posts: 12,386
  • Joined: 12-December 12

Re: Multiple and

Posted 19 December 2013 - 02:33 PM

If all the conditions are correct then

GetVariable ("T04_LID_SCREW1") <> "PRESENT"

will be true and only this single elseif will be executed. It will ignore, and skip past, all the other elseif clauses.

I think you really need to take the time to understand if/elseif.
Was This Post Helpful? 0
  • +
  • -

#11 Roland_J  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-August 13

Re: Multiple and

Posted 19 December 2013 - 11:09 PM

Thanks. I'm still looking for good examples.

For starters, why is any of it being evaluated if "variant" <> "GS1", in the first line.
I get the same result whatever "variant" is. I expected the entire block to be ignored if variant = GS2, say, which would be another block.

Regards
Roland

This post has been edited by andrewsw: 20 December 2013 - 02:13 AM
Reason for edit:: Removed previous quote

Was This Post Helpful? 0
  • +
  • -

#12 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,781
  • Joined: 26-March 09

Re: Multiple and

Posted 20 December 2013 - 01:09 AM

View PostRoland_J, on 20 December 2013 - 06:09 AM, said:

Thanks. I'm still looking for good examples.

For starters, why is any of it being evaluated if "variant" <> "GS1", in the first line.
I get the same result whatever "variant" is. I expected the entire block to be ignored if variant = GS2, say, which would be another block.

Regards
Roland


Couple of things.

1. Are you saying it needs to pass the Variant test before continuing? If that is the case, then the way you have the code won't work, as you're using the check on Variant within the If-ElseIf statements, so as soon as that is evaluated successfully, it exits the If-ElseIf block. You should try something like.

if GetVariable("variant") = "GS1" then
   'We passed the variant test, so now check out the component parts
  
  if GetVariable ("T04_LID_SCREW1") <> "PRESENT" then
     setvariable "final_result","NO LID SCREW 1"

  elseif GetVariable ("T05_HINGE_SCREW2") <> "PRESENT" then
     setvariable "final_result","NO HINGE SCREW 2"

  elseif GetVariable ("T06_HINGE_SCREW3") <> "PRESENT" then
     setvariable "final_result","NO HINGE SCREW 3"

  elseif GetVariable ("T07_LID_SCREW6") <> "PRESENT" then
     setvariable "final_result","NO LID SCREW 6"

  elseif GetVariable ("T08a_GROMMET") <> "PRESENT" then
     setvariable "final_result","MISSING GROMMET"

  else 
     setvariable "final_result", "RESULT PASS"
  end if

else
  'Failed variant test
  setvariable "final_result","Not GS1 Variant"
end if




2. What is the return type of GetVariable? Have you stepped this through in debug mode to ensure it's returning the correct results? Andrew as a link in his signature on how to debug if you're unsure.

This post has been edited by maj3091: 20 December 2013 - 01:10 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1