COMPARE 1 varible with many Variables

Just starting out? Need help? Post your questions and find answers here.
bidanh00co
User
User
Posts: 56
Joined: Thu Jul 07, 2005 10:06 am

COMPARE 1 varible with many Variables

Post 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.
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

make sure a is declared as a string first.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: COMPARE 1 varible with many Variables

Post 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 ...
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Jellybean
User
User
Posts: 95
Joined: Wed Aug 24, 2005 7:33 pm

Re: COMPARE 1 varible with many Variables

Post 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
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post 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
Anthony Jordan
User avatar
Fou-Lu
Enthusiast
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

Post 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'. :wink:

~Fou-Lu (aka Lørd Cinneris (actually Elias Sant'Ana))

Image Image
Post Reply