Jump to content

Variables


Recommended Posts

Hi all..I have a question. hope u all can help me solve. Thanks =)

Sleep("1000")
Send("!m""n""n")
Sleep("1000")
Send("TECHNICAN") ; For TECHNICAN
Send("{ENTER}")

Sleep("1000")
Send("!m""n""n")
Sleep("1000")
Send("OFFICER") ; For OFFICER
Send("{ENTER}")

Sleep("1000")
Send("!m""n""n")
Sleep("1000")
Send("STUDENT") ; For STUDENT
Send("{ENTER}")

Sleep("1000")
Send("!m""n""n")
Sleep("1000")
Send("TEENAGER") ; For TEENAGER
Send("{ENTER}")

Sleep("1000")
Send("!m""n""n")
Sleep("1000")
Send("ELDERLY") ; For ELDERLY
Send("{ENTER}")

All these five steps i need to put them into a loop. But at the 4th line, there is a difference in the command for each of the 5 Steps. I can set variable for each of the wordings like this: $sss = 'TECHNICAN', $eee = 'OFFICER', $rrr = 'STUDENT', $fff = 'TEENAGER', $yyy = 'ELDERLY'

But how do i make sure that for the loop, it will know which variable to take??..i want all the five steps to be in one loop. I know that the command Send("ELDERLY") can be replaced by Send("$yyy") . But how can i make sure that the com knows which variable to take?..hoep someone can guide me on this..Thanks.. =)

Link to comment
Share on other sites

here is a hopefully descent bit of example code for you

Basically, make the values (technician, officer etc) into an array, then step through the array in a for loop.

#Include <array.au3>

; Set $y to the number of elements in the array
$y = 6

; create the array, set $userarray as a global variable first
dim $userarray[$y]
$userarray = _ArrayCreate($y, "technician", "officer", "student", "teenager", "elderly")

; display the array to ensure it is correct (may be removed later)
_ArrayDisplay($userarray, "test")

; the loop steps through a series of commands, the send command to send the value of part of the array is included.
For $x = 1 to $userarray[$y] step 1
    ; your commands
    Send($userarray[$x])
Next

Exit
Edited by tAKTelapis
Link to comment
Share on other sites

Hi tAKTelapis. Thanks for ur help. But i've tried the script that u gave me. It can't seem to work out.

#Include <array.au3>

Run("notepad.exe")
WinWaitActive("Untitled")

$y = 6
dim $userarray[$y]
$userarray = _ArrayCreate($y, "technician", "officer", "student", "teenager", "elderly")

For $x = 1 to $userarray[$y] step 1
    Send("hello.......")
    Sleep("1000")
    Send("{ENTER}")
    Send($userarray[$x])
    Send("{ENTER}")
Next

Exit
Link to comment
Share on other sites

  • Moderators

Hi tAKTelapis. Thanks for ur help. But i've tried the script that u gave me. It can't seem to work out.

#Include <array.au3>

Run("notepad.exe")
WinWaitActive("Untitled")

$y = 6
dim $userarray[$y]
$userarray = _ArrayCreate($y, "technician", "officer", "student", "teenager", "elderly")

For $x = 1 to $userarray[$y] step 1
    Send("hello.......")
    Sleep("1000")
    Send("{ENTER}")
    Send($userarray[$x])
    Send("{ENTER}")
Next

Exit
Because it's setup wrong... you could do...

For $x = 1 to $userarray[0] step 1

Or

For $x = 1 to UBound($userarray) - 1 step 1

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Hi SmOke_N. Thanks. But i've got a question in mind too. I know that:

$userarray[0] = $y

$userarray[1] = 'technican'

$userarray[2] = 'officer'

$userarray[3] = 'student'

$userarray[4] = 'teenager'

$userarray[5] = 'elderly'

By using this script:

#Include <array.au3>

Run("notepad.exe")
WinWaitActive("Untitled")

$y = 5
dim $userarray[$y]
$userarray = _ArrayCreate($y, "technician", "officer", "student", "teenager", "elderly")

For $x = 1  to $userarray[0] step 1
    Send("hello.......")
    Sleep("1000")
    Send("{ENTER}")
    Send($userarray[$x])
    Send("{ENTER}")
Next

Exit

Why does the For Loop state that For $x = 1 to $userarray[0] step 1, and i know that $x increases everytime it goes through the loop. But why $userarray[0] should start with 0?..i thought that should be For $x = 1 to $userarray[5] step 1, because it says step 1 and it should increases everytime. I thought For is the begining variable and the To is then end variable??..am i correct??..hope u can enlighten me..thanks.. =)

Link to comment
Share on other sites

sorry about the initial mistake that i made guys.

smoke_n pointed it out, i made the example in a rush

@cedrIck - The reasone for doing:

For $x = 1 to $userarray[0] step 1

is that $userarray[0] contains the value which sets the number of elements in the array (in this case, there are 6 elements, with element 1 being "technician" and element 0 being the TOTAL number of elements in the array).

this value is determined by the variable $y which you can set yourself at the top of the script (i did this to make it easier to change the number of elements in the array

$y should equal 6 as the array is inclusive of $userarray[0] (making a total of 6 values in the array 0,1,2,3,4,5) with value 0 equal to 6.

The "_ArrayDisplay($userarray, "test")" in my original script shows this.

Link to comment
Share on other sites

Hi tAKTelapis again. I understand ur point le..But i have another question. Hope u can reply me fast. Thanks =)

#Include <array.au3>

Run("notepad.exe")
WinWaitActive("Untitled")

$y = 5
dim $userarray[$y]
$userarray = _ArrayCreate($y, "technician", "officer", "student", "teenager", "elderly")

For $x = 1  to $userarray[0] step 1
    Send("hello.......")
    Sleep("1000")
    Send("{ENTER}")
    Send($userarray[$x])
    Send("{ENTER}")
Next

Exit

In the first place u put $y=6, the script can run but it came out an error : C:\Documents and Settings\Administrator\Desktop\111111.au3 (16) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

Send($userarray[$x])

Send(^ ERROR

So i changed $y=5, then the whole script can run without error. So what i wan to ask is that izzit $y=5 is correct??..thanks..

Link to comment
Share on other sites

Hmm... i see your dilema..

And now i know where i went wrong

For $x = 1 to $userarray[0] step 1

the initial 1 indicates that the script should start at array element $userarray[1] and add +1 on it until $x is equal to the value in $userarray[0] (which i set to 6) however, using that number will cause the script to try and run for $userarray[6] which doesnt exist. (we only want to run it to $userarray[5])

my mistake again, 5 is indeed the correct number to be using. (i managed to confuse myself there)

Link to comment
Share on other sites

Hmm... i see your dilema..

And now i know where i went wrong

For $x = 1 to $userarray[0] step 1

the initial 1 indicates that the script should start at array element $userarray[1] and add +1 on it until $x is equal to the value in $userarray[0] (which i set to 6) however, using that number will cause the script to try and run for $userarray[6] which doesnt exist. (we only want to run it to $userarray[5])

my mistake again, 5 is indeed the correct number to be using. (i managed to confuse myself there)

Hey tAKTelapis. don't say tat. You indeed helped me a lot. Thanks a million!! Now i know that it starts from $userarray[1] coz its $x=1, yup..thanks once again. You saved my life..haha..Good to know you in this forum!. Cya. =)

Link to comment
Share on other sites

Hi tAKTelapis. I'am sorrie to trouble u again. I just thought of another way of doing it. Can u help me check whether this can be done this way too??..thanks.. =)

#Include <array.au3>

Run("notepad.exe")

WinWaitActive("Untitled")

$y = 5

dim $userarray[$y]

$userarray = _ArrayCreate($y, "technician", "officer", "student", "teenager", "elderly")

For $x = 1 to 5 step 1

Send("hello.......")

Sleep("1000")

Send("{ENTER}")

Send($userarray[$x])

Send("{ENTER}")

Next

Exit

Izzit possible to change $userarray[0] to the number 5??..it still gives me the same result. But i just wana make sure if this way can be used for this script. izzit possible?..anything wrong??..

Link to comment
Share on other sites

  • Moderators

You have to add 1 to the Dim...

dim $userarray[$y + 1]

You are adding 5 to the [0] element with $y.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

CedrIck,

Yes, the way you have shown will work..

as the data inside $userarray[0] contains the number 5 as its data.

you have taken the alternate route, and simply hard-coded the number into your For....Next statement

It WILL work, however, it isn't usually considered good practice. As in the event you ever wanted to add an extra value to the array (say, "guest") then you would have to add "guest" to your array, and find the for...next loop in your code (if you have had a look around, you will see there are some really large scripts and identifying just one number in it all can get annoying) so having it set at the very top of the script, means its easy to update.

@smoke_n

you are obviously more seasoned at this than me... so i have to ask, how does what you have said result in $userarray[0] being equal to 5? it would put it back upto 6 would it not?

i would have interpretted cedrIck point as wanting to just hard-set value [0]

ie:

$userarray = _ArrayCreate(5, "technician", "officer", "student", "teenager", "elderly")

cedrIck that ^ will result in $userarray[0] being equal to 5 without having to set it based on a variable.. however, when you do the "dim $userarray[$y]" you might need to remove the "[$y]" thought im not entirely sure.. i am un-able to test it.

Link to comment
Share on other sites

  • Moderators

@smoke_n

you are obviously more seasoned at this than me... so i have to ask, how does what you have said result in $userarray[0] being equal to 5? it would put it back upto 6 would it not?

You set $y at 5, but you have 6 elements 0 through 5.

Then you set the array element max at 5, so it's going to error, thus Dim $array[$y + 1] (telling the interpreter you have 6 elements).

Also, you hard coded [0] element with the already defined $y with 5....

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

yup, got the last part, but now i see what you are getting at with the dim $array[$y + 1] makes a whole lot more sense

cheers for the info smoke_n

No problem, I can't remember the thread or the link, but Uten has a really good link for understanding arrays. Maybe you could PM him for it. I'll find it again one day I'm sure :whistle:

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

What might be useful in this particular case is a not-so-often used For variant:

For...In...Next (search the index in the help, also for example script). Seems like this might be more useful in your case, since you will not have to use arrays here.

Though it is GOOD to practise using arrays in scripts, because typically the more you code the more you will come across them. But in this partiucular case, the For...In...Next will save you the need for understanding the difference between 0- and 1-based arrays and max indexes etc..

Cheers. Happy learning!

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...