Page 1 of 1
COMPARE 1 varible with many Variables
Posted: Sat Aug 27, 2005 3:42 am
by bidanh00co
Hi all,
1/. Is it possible to make this code shorter ??
IF a <> var1$ AND a <> var2$ AND a <> var3$ AND a <> var4$ AND a <> var5$ AND ......... (very long)
----------------
----------------
EndIf
2/. Can we make the code above look like this ??
IF a <> var1$
AND a <> var2$
AND a <> var3$
AND a <> var4$
AND a <> var5$
-----------
-----------
EndIF
3/. I tested the first question with LINKEDLIST(). But no result. I dun know why??!!
NewList Listvar.s()
AddElement(Listvar())
Listvar()="asdsadc"
AddElement(Listvar())
Listvar()="werwqe"
AddElement(Listvar())
Listvar()="etryrtr"
ForEach Listvar()
If a <> Listvar()
----------
----------
EndIf
Is this code do the same like the first code ??
Bidanh00co.
Posted: Sat Aug 27, 2005 3:54 am
by dracflamloc
make sure a is declared as a string first.
Re: COMPARE 1 varible with many Variables
Posted: Sat Aug 27, 2005 4:27 am
by PB
> Is it possible to make this code shorter ??
> IF a <> var1$ AND a <> var2$ AND a <> var3$ AND a <> var4$ AND a <> var5$ AND ......... (very long)
Nope. You can't split it over several lines, either. However, I'd just store all
the var items in an array, and then the line above could be done in a much
more simpler way like this:
Code: Select all
Dim var$(50) ; Assume there are 50 items to be checked against a.
m=0 : For c=1 To 50 : If a=var$(c) : m=1 : Break : EndIf : Next
If m=0
; a did not match any of the 50 variables.
Else
; a matched at least one of the 50 variables.
EndIf
So you see, you can test a against any large number of variables in the array,
without having to have a long line of: IF a <> var1$ AND a <> var2$ AND ...
Re: COMPARE 1 varible with many Variables
Posted: Sat Aug 27, 2005 11:39 am
by Jellybean
bidanh00co wrote:Hi all,
1/. Is it possible to make this code shorter ??
IF a <> var1$ AND a <> var2$ AND a <> var3$ AND a <> var4$ AND a <> var5$ AND ......... (very long)
Not necessarily shorter, but this is more readable:
Code: Select all
Select a
Case var1$
Case var2$
Case var3$
Case var4$
Case var5$
Default
;Your code here
EndSelect
Posted: Sat Aug 27, 2005 6:33 pm
by akj
Here is a possible way of shortening the code:
Code: Select all
d.s="|" ; Delimiter character, assumed not to be present in any variable varN$
all$=d+var1$+d+var2$+d+var3$+d+var4$+d+var5$+d
If FindString(all$, d+a+d, 1)
Debug "a equals at least one of the variables"
Else
Debug "a is not equal to any of the variables"
EndIf
Re: COMPARE 1 varible with many Variables
Posted: Sat Aug 27, 2005 7:09 pm
by Fou-Lu
bidanh00co wrote:2/. Can we make the code above look like this ??
IF a <> var1$
AND a <> var2$
AND a <> var3$
AND a <> var4$
AND a <> var5$
-----------
-----------
EndIF
You could do this:
IF a <> var1$
IF a <> var2$
IF a <> var3$
IF a <> var4$
IF a <> var5$
(...)
EndIF:EndIF:EndIF:EndIF:EndIF
But I think you should verify any change on these strings at the time you modify them. Let's say you have 50 var$'s, everytime you modify one of them, you compare it with 'a':
Code: Select all
Global a.s
Global b.l
Dim var.s(50)
Procedure ChangeValue(index.l,newvalue.s); I've got no better name...
if var(index)=a and newvalue<>a
b=b-1:endif
if var(index)<>a and newvalue=a
b=b+1:endif
var(index)=newvalue
EndProcedure
;Now everytime you want to change the value, use the procedure above!
ChangeValue(34,"blah")
;Everytime you need to know if any of them if different than 'a' you do like that:
If b=0 ; 'b' being zero means that no 'var$' is equal to 'a'
(...)
EndIf
That way you will always know how many var$'s are equal to 'a'.
