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

##### 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
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][/list][/font]

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

##### Share on other sites

lod3n...

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

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

##### 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
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][1] = 1
Else
\$animals[\$found][1] += 1
EndIf
wend```

[font="Fixedsys"][list][/list][/font]

##### Share on other sites

And if you can figure out why the first version worked despite the bug (it's actually quite simple) you win a cookie.

[font="Fixedsys"][list][/list][/font]

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

##### Share on other sites

Ah, += and &= may be something that only works in the beta.

[font="Fixedsys"][list][/list][/font]

## Create an account

Register a new account