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.
COMPARE 1 varible with many Variables
-
bidanh00co
- User

- Posts: 56
- Joined: Thu Jul 07, 2005 10:06 am
-
dracflamloc
- Addict

- Posts: 1648
- Joined: Mon Sep 20, 2004 3:52 pm
- Contact:
Re: COMPARE 1 varible with many Variables
> 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:
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 ...
> 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
without having to have a long line of: IF a <> var1$ AND a <> var2$ AND ...
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
"PureBasic won't be object oriented, period" - Fred.
Re: COMPARE 1 varible with many Variables
Not necessarily shorter, but this is more readable: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)
Code: Select all
Select a
Case var1$
Case var2$
Case var3$
Case var4$
Case var5$
Default
;Your code here
EndSelectHere 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
Anthony Jordan
- Fou-Lu
- Enthusiast

- Posts: 201
- Joined: Tue Jul 12, 2005 8:30 am
- Location: I'm pretty sure this is all a nightmare
- Contact:
Re: COMPARE 1 varible with many Variables
You could do this: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
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