Jump to content

Noob 2-Dimensional array question


Recommended Posts

Baby stepping up to 2-dimensional arrays.

Example:

If I have a 1-dimensional array of animals, which may have entries added or removed,

$animals[0] = "Dog"

$animals[1] = "Cat"

$animals[2) = "Bird"

$animals[3] = "Fish"

and I have someone pick their favorite which is assigned to the string ($choice).

How do I create & increment a 2-dimensional array to reflect cumulative favorites ?

(Starting code with all of its glaring errors.....)

$found = 0

Global $favorites[0][0] = 0

While 1

For $a = 1 To $favorites[0][0]

If $favorites[$a][0] = $choice Then

$favorites[$a][1] = $favorites[$a][1] + 1

$found = 1

ExitLoop

EndIf

Next

If $found = 1 Then

$found = 0

ExitLoop

Else

; ??? code to add new elements to array ???

EndIf

Wend

Thanks in advance for any help getting me on the right track...

Link to comment
Share on other sites

Try this:

dim $animals[4][2]

$animals[0][0] = "Dog"
$animals[1][0] = "Cat"
$animals[2][0] = "Bird"
$animals[3][0] = "Fish"

$animals[0][1] = 0
$animals[1][1] = 0
$animals[2][1] = 0
$animals[3][1] = 0

while 1
    $question = "Please type your favorite animal: "
    for $i = 0 to ubound($animals,0)+1
        $question &= @crlf & @tab & $animals[$i][0] & " [Count: " & $animals[$i][1] & " ]"
    Next
    $answer = InputBox ( "", $question)
    if $answer = "" then ExitLoop

    $found = -1
    for $i = 0 to ubound($animals,0)+1
        if $answer = $animals[$i][0] then $found = $i
    Next
    if $found = -1 then
        msgbox(16,"Error","I've never heard of that animal.")
    Else
        $animals[$found][1] += 1
    EndIf
wend

[font="Fixedsys"][list][*]All of my AutoIt Example Scripts[*]http://saneasylum.com[/list][/font]

Link to comment
Share on other sites

Try this:

dim $animals[4][2]

$animals[0][0] = "Dog"
$animals[1][0] = "Cat"
$animals[2][0] = "Bird"
$animals[3][0] = "Fish"

$animals[0][1] = 0
$animals[1][1] = 0
$animals[2][1] = 0
$animals[3][1] = 0

while 1
    $question = "Please type your favorite animal: "
    for $i = 0 to ubound($animals,0)+1
        $question &= @crlf & @tab & $animals[$i][0] & " [Count: " & $animals[$i][1] & " ]"
    Next
    $answer = InputBox ( "", $question)
    if $answer = "" then ExitLoop

    $found = -1
    for $i = 0 to ubound($animals,0)+1
        if $answer = $animals[$i][0] then $found = $i
    Next
    if $found = -1 then
        msgbox(16,"Error","I've never heard of that animal.")
    Else
        $animals[$found][1] += 1
    EndIf
wend
Thanx for the super quick reply lod3n.

I was going to check back this evening.

I will plug this in asap and study it's intricacies

Thanx again.... :P

Link to comment
Share on other sites

lod3n...

It works great for incrementing choices when they pick a listed animal.

How would I go about adding their unlisted addition to the list ??

I would think that it would go where msgbox(16,"Error", "I've never heard of that animal.") is listed.

Link to comment
Share on other sites

Woo, I had a pretty bad bug in the first version of this code. I'm suprised it worked at all. I was doing insane things with Ubound. Here it is, sans bugs, plus adding animals:

dim $animals[4][2]

$animals[0][0] = "Dog"
$animals[1][0] = "Cat"
$animals[2][0] = "Bird"
$animals[3][0] = "Fish"

$animals[0][1] = 0
$animals[1][1] = 0
$animals[2][1] = 0
$animals[3][1] = 0

while 1
    $question = "Please type your favorite animal: "
    for $i = 0 to ubound($animals,1)-1
        $question &= @crlf & @tab & $animals[$i][0] & " [Count: " & $animals[$i][1] & " ]"
    Next
    $answer = InputBox ( "", $question)
    if $answer = "" then ExitLoop

    $found = -1
    for $i = 0 to ubound($animals,1)-1
        if $answer = $animals[$i][0] then $found = $i
    Next
    
    if $found = -1 then
        ;msgbox(16,"Error","I've never heard of that animal.")
        
        $max = ubound($animals,1)
        ReDim $animals[$max+1][2]
        $animals[$max][0] = $answer
        $animals[$max][1] = 1
    Else
        $animals[$found][1] += 1
    EndIf
wend

[font="Fixedsys"][list][*]All of my AutoIt Example Scripts[*]http://saneasylum.com[/list][/font]

Link to comment
Share on other sites

lod3n

I'm not sure why it pretty much worked.

I guess because we were trapping for the problem with the msgbox inadvertently ?

Not sure if your's works but had to change:

$question &= @crlf & @tab & $animals[$i][0] & "[Count: " & $animals[$i][1] & "]"

TO--

$question = @crlf & @tab & $animals[$i][0] & "[Count: " & $animals[$i][1] & "]"

And,

$animals[$found][1] += 1

TO--

$animals[$found][1] = $animals[$found][1] + 1

Otherwise, I think it works....

Thanks again

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...