Sign in to follow this  
Followers 0
darkshadow791

If loops and arrays...

8 posts in this topic

I was wondering if there was an easy way of having an if loop based on an array... I'm not good at explaining very well...

Basically I have a really long array (I.E. $array[0] - $array[100] or something like that) And I want to make an if loop that will do a certain function based on what $array returns... i.e. if $array[0] is returned true it will do func(0) or $array[1] will do func(1) etc. It can be something besides an array if there's an easier way of doing it... I basically want something like this that can go to 200 or w/e I please without having to copy and paste it 200 times...

Share this post


Link to post
Share on other sites



hmm, it sounds like a for loop is what you want to use.

Maybe post some code or explain what you're doing exactly, might be able to find a more efficent way of doing it.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I was wondering if there was an easy way of having an if loop based on an array... I'm not good at explaining very well...

Basically I have a really long array (I.E. $array[0] - $array[100] or something like that) And I want to make an if loop that will do a certain function based on what $array returns... i.e. if $array[0] is returned true it will do func(0) or $array[1] will do func(1) etc. It can be something besides an array if there's an easier way of doing it... I basically want something like this that can go to 200 or w/e I please without having to copy and paste it 200 times...

for $x = 0 to Ubound($array) - 1
    $value = $array[$x]
    if $value = 'true' then
        true_func()
    elseif $value = 'false' then
        false_func()
    endif
next

Although we dont mind helping those who require it, you should really become accustomed to searching through the helpfile. A simple search on arrays and conditionals would have given you all that you needed to know to complete this snippet of code.

Edited by ame1011

[font="Impact"] I always thought dogs laid eggs, and I learned something today. [/font]

Share this post


Link to post
Share on other sites

I don't actually have the code yet I'm planning a project in the future but I was planning on having a value (lets say $x) be user inputted and based on that value it will run a func($x) and based on the value of func($x) ( $y = func($x) )it will return another func, func2($y) and I was seeing if there was a simple way to have it set it so I don't have to copy / paste it... I knew about for loop's but I'm not very good with those and I did look through the help file but I couldn't really find a concrete answer... But I think I might be able to figure it out based on what you guy's posted, thanks.

Ex. if you couldn't figure out my descript:

$x = userinputbox ( blah blah blah )

func($x)

$y = func($x)

If $y = 1

func2(1)

If $y = 2

func2(2)

etc...

Share this post


Link to post
Share on other sites

I don't actually have the code yet I'm planning a project in the future but I was planning on having a value (lets say $x) be user inputted and based on that value it will run a func($x) and based on the value of func($x) ( $y = func($x) )it will return another func, func2($y) and I was seeing if there was a simple way to have it set it so I don't have to copy / paste it... I knew about for loop's but I'm not very good with those and I did look through the help file but I couldn't really find a concrete answer... But I think I might be able to figure it out based on what you guy's posted, thanks.

Ex. if you couldn't figure out my descript:

$x = userinputbox ( blah blah blah )

func($x)

$y = func($x)

If $y = 1

func2(1)

If $y = 2

func2(2)

etc...

You need to take much more, or much less, of whatever it is you're on... :)

While recovering from the [overdose|withdrawl], how about coding a quick example of what you're talking about, with only say three elements? Get it working in small scale (with help if needed) then scale it up to hundreds of "functions".

:P


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

I have no clue what your after but this might give you somthing to think about.

It is not tested :)

$arr[0][0] = 0
$arr[0][1] = 'func0'
$arr[1][0] = 1
$arr[1][1] = 'func1'
Func ArrayCallLoop(ByRef $arr)
    Local $i
    For $i = 0 to UBound($arr) - 1
        if $arr[$i][0] then call $arr[$i][1]
    Next
EndFunc 
Func func0()
    ; Do Work
EndFunc 
Func func1()
    ; Do Work
EndFunc

Share this post


Link to post
Share on other sites

why only 100 functions? I say go for 1000


[font="Impact"] I always thought dogs laid eggs, and I learned something today. [/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