Sign in to follow this  
Followers 0
daniel02

array compare with variable

15 posts in this topic

Hi all,

I just started with all these scripting stuff but I am stuck at the moment. I want to comapre a static variable with an array in if --- endif

I want to compare all array values but I can not find what string is used for it. I knew it from ksh scripting where you can say to get all array values ${arr[*]}

$var1 = 1

Local $arr[3] = ["element 1", "element 2", "element 3"]

if $var1 = ${arr[*]} then

...

endif

So just compare if $var1 has the same value as one of the array values and if it is the same value do somethig.

Thanks a lot in advance

Daniel

Share this post


Link to post
Share on other sites



use double [

Local $arr[3] = [["element 1", "element 2", "element 3"]]

Share this post


Link to post
Share on other sites

@JamesBrooks as far as I know I only compare array value1 with the variable and not every value in the array?

@Quizzy If i use double I think the whole expression is than one value.. But i need them seperated. Or am I wrong?

cheers

Daniel

Share this post


Link to post
Share on other sites

I do not want to do something like

If $var = $arr[1] OR $var = $arr[2] OR $var = $arr[3] then

Share this post


Link to post
Share on other sites

What the hell Quizzy?

Daniel, if you want to check all variables do this:

Local $arr[4] = [3, "element 3", "element 2", "element 3"]
Local $var = 1

For $i = 0 to $arr[0]
    ConsoleWrite($arr[$i] & @CRLF)
    If StringInStr($arr[$i], String($var)) Then 
        MsgBox(0, "", "Match at: " & $arr[$i] & "!")
    Else
        ConsoleWrite("No match found with: " & $arr[$i] & @CRLF)
    EndIf
Next

I know you could use UBound() but I find it easier this way when using arrays which don't change size.

Share this post


Link to post
Share on other sites

Thanks a lot James. So there is no way to just Wildcard it with $arr[*]? I find this is very complicated but anyway understandable.

One more (and hopefully last) question:

What is "& @CRLF"

Thanks again

daniel

Share this post


Link to post
Share on other sites

You could also use _ArraySearch and check for returnvalue >= 0.

"string" & @CRLF means:

& -> concatenate two strings

@CRLF -> Macro for Carriage return + Linefeed (the line separator chars for Windows)


*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

What the hell Quizzy?

Daniel, if you want to check all variables do this:

Local $arr[4] = [3, "element 3", "element 2", "element 3"]
Local $var = 1

For $i = 0 to $arr[0]
    ConsoleWrite($arr[$i] & @CRLF)
    If StringInStr($arr[$i], String($var)) Then 
        MsgBox(0, "", "Match at: " & $arr[$i] & "!")
    Else
        ConsoleWrite("No match found with: " & $arr[$i] & @CRLF)
    EndIf
Next

I know you could use UBound() but I find it easier this way when using arrays which don't change size.

Which is fine for this example, but it could easily be misunderstood by some that arrays are 'always' built with [0] element containing an updated value corresponding to the array's size. While most built in functions use that standard, not every array is built that way. Using UBound() is much safer, especially when giving instruction to a novice.... But that is just my pompous opinion :) Edited by SpookMeister

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Share this post


Link to post
Share on other sites

@spookymeister so could you give me an examlpe how to solve my problem with ubound?

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Using James' example...

Local $arr[4] = [3, "element 3", "element 2", "element 3"]
Local $var = 1

For $i = 0 to UBound($arr) - 1
    ConsoleWrite($arr[$i] & @CRLF)
    If StringInStr($arr[$i], String($var)) Then
        MsgBox(0, "", "Match at: " & $arr[$i] & "!")
    Else
        ConsoleWrite("No match found with: " & $arr[$i] & @CRLF)
    EndIf
Next

[edit] Fixed my screwed up example... sheesh... I need to wake up before interfacing with the world

Edited by SpookMeister

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

SpookMeister, that's why I mentioned when it's a fixed size. Sure, I should have put a note there explaining why.

Sorry if I came across as picking at you, I was mostly just being an ass (still drinking my first cup of coffee here)... no insult/disrespect was intended :) Edited by SpookMeister

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Sorry if I came across as picking at you, I was mostly just being an ass (still drinking my first cup of coffee here)... no insult/disrespect was intended :)

Oh no problem, takes a lot for me to get offended these days :)

Nice example too, however when I tried it, it errored (my attempt) nevermind.

Oh, you forgot to remove the 3 from the array.

Local $arr[3] = ["element 3", "element 2", "element 3"]
Local $var = 1

For $i = 0 to UBound($arr) -1
    ConsoleWrite($arr[$i] & @CRLF)
    If StringInStr($arr[$i], String($var)) Then 
        MsgBox(0, "", "Match at: " & $arr[$i] & "!")
    Else
        ConsoleWrite("No match found with: " & $arr[$i] & @CRLF)
    EndIf
Next
Edited by JamesBrooks

Share this post


Link to post
Share on other sites

use double [

Local $arr[3] = [["element 1", "element 2", "element 3"]]

That form of initialization would only be appropriate for a 2D array:
#include <Array.au3>; Only for _ArrayDisplay()

Local $arr[2][3] = [["element 1A", "element 1B", "element 1C"], ["element 2A", "element 2B", "element 2C"]]
_ArrayDisplay($arr, "$arr 2D array")

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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