Sign in to follow this  
Followers 0
AutID

Comparing variables

36 posts in this topic

Hello,

$1 = "Q"
$2 = "W"
$3 = "Y"
$4 = "U"
$5 = "S"

$c = "Y"

 

Is there a way to compare $c with these variables and find which one is the right one without having to use an if/elseif/endif statement???

I tried a stringinstr but not much success.

Share this post


Link to post
Share on other sites



AutId,

Here is one way:

$1 = "Q"
$2 = "W"
$3 = "Y"
$4 = "U"
$5 = "S"

$c = "Y"

for $1 = 1 to 5
    if eval($1) = $c then consolewrite('$' & $1  & ' is equal' & @lf)
next

However, if you are interested in storing values for later lookup there are much better techniques than individual variables.  If you explain a little about what you are doing you will receive a better answer.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

StringCompare

AutID,

Your script doesn't show, than "if/elseif/endif" is bad

$1 = "Q"
$2 = "W"
$3 = "Y"
$4 = "U"
$5 = "S"

$c = "Y"
If $1 = "Q" And $2 = "W" And $3 = "Y" And $4 = "U" And $5 = "S" Then MsgBox(0, ';)', 'Yes')
Edited by AZJIO

Share this post


Link to post
Share on other sites

kylomas,

What if $c is "Q"?

AZJIO,

AutID wants to compare $c to $1 to $5. What is your example doing, exactly?

AutID,

It would be easier to use an array:

#Include <Array.au3>

Local $Values[5] = ["Q", "W", "Y", "U", "S"]
Local $c = "Y"

Local $Index = _ArraySearch($Values, $c)

If $index >= 0 Then
    ConsoleWrite("$c was found in $Values at index " & $index & @LF)
Else
  ConsoleWrite("$c was not found in $Values" & @LF)
EndIf

You can also use Select...Case...EndSelect.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

@jchd - Ha! How stupid, I overwrite the value of $1...thanks.

However, if you are interested in storing values for later lookup there are much better techniques than individual variables. If you explain a little about what you are doing you will receive a better answer.

 

This stands however.  You used an array but it could also be a string.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

OP has explicitly requested "without having to use an if / elseif / endif statement"

maybe he meant something like this (no command IF it's required)

$1 = "Q"
$2 = "W"
$3 = "Y"
$4 = "U"
$5 = "S"

$c = "Y"

dim $oracle[2] = ["not equal","equal"]

for $1 = 1 to 5
    ConsoleWrite("$" & $1 & " " & $oracle[(eval($1) == $c)] & " $c" & @crlf)
next

bye


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

@PincoPanco, you have a bug in your script. This will prevent your script from comparing $c to $1 so if $c = "Q", the script displays all as "not equal".

$1 = "Q"
$2 = "W"
$3 = "Y"
$4 = "U"
$5 = "S"

$c = "Y"

dim $oracle[2] = ["not equal","equal"]

for $1 = 1 to 5 ; <<<<<<<<<<<<<<<<<<<<< you're reusing the variable $1
    ConsoleWrite("$" & $1 & " " & $oracle[(eval($1) == $c)] & " $c" & @crlf)
next
1 person likes this

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

ops
BrewManNH you're right
I modified
kylomas's example from post >#2 without changing the loop variable
using a different loop variable name works well

$1 = "Q"
$2 = "W"
$3 = "Y"
$4 = "U"
$5 = "Y"

$c = "Q"

dim $oracle[2] = ["not equal","equal"]

for $X = 1 to 5
    ConsoleWrite("$" & $X & " " & $oracle[(eval($X) == $c)] & " $c" & @crlf)
next

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

kylomas,

What if $c is "Q"?

AZJIO,

AutID wants to compare $c to $1 to $5. What is your example doing, exactly?

AutID,

It would be easier to use an array:

#Include <Array.au3>

Local $Values[5] = ["Q", "W", "Y", "U", "S"]
Local $c = "Y"

Local $Index = _ArraySearch($Values, $c)

If $index >= 0 Then
    ConsoleWrite("$c was found in $Values at index " & $index & @LF)
Else
  ConsoleWrite("$c was not found in $Values" & @LF)
EndIf

You can also use Select...Case...EndSelect.

Tu es le seul qui a compris ;)

I came up with this after jchd's sample.

#Include <Array.au3>
$1 = "Q"
$2 = "W"
$3 = "Y"
$4 = "U"
$5 = "S"
Local $Values[5] = [$1, $2, $3, $4, $5]
Local $c = "Y"
Local $Index = _ArraySearch($Values, $c)
ConsoleWrite($Values[$index] & @LF)

Share this post


Link to post
Share on other sites

I thought you wanted to avoid using If/ElseIf/Else? _ArraySearch is loaded with them. :)


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

I thought you wanted to avoid using If/ElseIf/Else? _ArraySearch is loaded with them. :)

They are not in my script though ;)

Share this post


Link to post
Share on other sites

Actually, yes they are, they aren't in what you wrote, but they're definitely in your script.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Actually, yes they are, they aren't in what you wrote, but they're definitely in your script.

You know what I mean...

Share this post


Link to post
Share on other sites

I agree with BrewManNH That _ArraySearch is full of "IF"
(you do not see them only because they hid behind #Include <Array.au3> :P
and what will happens to your code if "Y" are more than 1, or even worse, none?

well, allowing the use of #include <Array.au3>, can you manage that cases? (without the "IF" statement ofcourse)
if you need a tip as well ask ;)


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

wow, is this still hot or a competition ??? ;)

.

$1 = "Q"
$2 = "W"
$3 = "Y"
$4 = "U"
$5 = "S"

$c = "Y"

$string=$1&$2&$3&$4&$5

MsgBox(0,"",StringInStr($string,$c))

.

E.

Edit: of course, using includes is cheating. you could include every au3 script you want, even self written. this is cheating

Edited by Edano

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

and if you want casesense and occurence, take this:

.

$1 = "Q"
$2 = "W"
$3 = "Y"
$4 = "U"
$5 = "S"
$6 = "Y"

$c = "Y"

$i=0
$string=$1&$2&$3&$4&$5&$6

Do
    $i+=1
    $ret=StringInStr($string,$c,1,$i)
    MsgBox(0,"",$ret)
Until $ret=0

MsgBox(0,"",StringTrimRight("no (more)",7*($i=0))&" '"&$c&"' found.")

.

no "if"

Edit: Bool is not "If" :P

Edited by Edano

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

Share this post


Link to post
Share on other sites

and if you want casesense and occurence, take this:

.

$1 = "Q"
$2 = "W"
$3 = "Y"
$4 = "U"
$5 = "S"
$6 = "Y"

$c = "Y"

$i=0
$string=$1&$2&$3&$4&$5&$6

Do
    $i+=1
    $ret=StringInStr($string,$c,1,$i)
    MsgBox(0,"",$ret)
Until $ret=0

MsgBox(0,"",StringTrimRight("no (more)",6*($i=0))&" '"&$c&"' found.")

.

no "if"

Edit: Bool is not "If" :P

 

it finds one more, even if there are not


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

nonono, zero is defined as failure !


[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

Share this post


Link to post
Share on other sites

better without the view of that failure.... :whistle:


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

$1 = "Q"
$2 = "W"
$3 = "Y"
$4 = "U"
$5 = "S"
$6 = "Y"

$c = "Y"

$i=0
$string=$1&$2&$3&$4&$5&$6

Do
    $i+=1
    $ret=StringInStr($string,$c,1,$i)
    MsgBox(0,"",StringReplace($ret,"0","that's all"))
Until $ret=0

MsgBox(0,"",StringTrimRight("no (more)",7*($i=0))&" '"&$c&"' found.")

:thumbsup:

Edited by Edano

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0