x
Solved

# Getting confused with 2d arrays

Posted on 2008-06-11
Medium Priority
154 Views
Hi,

I just built a 2D array, and I want to iterate over all elements for printing. I tried this:

for (\$i = 0; \$i < @rows; \$i++) {
for (\$j = 0; \$j < @rows[\$i]; \$j++) {
print(\$rows[\$i][\$j], "  ");
}
print("\n");
}

where @rows is my 2D array, but the script just executes in an infinite loop. I'm guessing the problem is:

\$j < @rows[\$i]

in that statement I mean to say the length of the 1d array in element \$i of the 2D array. What's the right syntax for it?

thanks
0
Question by:DJ_AM_Juicebox

LVL 39

Expert Comment

ID: 21761331

``````for(my \$i = 0; \$i < @rows; \$i++) {
for (my \$j = 0; \$j < @{\$rows[\$i]}; \$j++) {
print(\$rows[\$i][\$j], "  ");
}
print("\n");
}
``````
0

LVL 13

Expert Comment

ID: 21761414
Here you'll find how to iterate the 2d array with example http://icfun.blogspot.com/2008/03/perl-two-dimensional-array-handling.html
0

Author Comment

ID: 21762045
Ehh still seems to just loop endlessly. Here's my entire script:

my @rows = ( [2, 4, 6],
[3, 6, 9],
);

for (my \$i = 0; \$i < @rows; \$i++) {
for (my \$j = 0; \$j < @{rows[\$i]}; \$j++) {
print(\$rows[\$i][\$j], "  ");
}
}
0

LVL 39

Accepted Solution

Adam314 earned 2000 total points
ID: 21762470
You are missing the \$ in front of rows[\$i].  It should be:
The @{\$rows[\$i]} means:
take \$rows[\$i], which is a reference to an array
Then take that array

Then the \$j < mean interpret that array in scalar context, which means return the number of elements in the array.
``````my @rows = ( [2, 4, 6],
[3, 6, 9],
);

for (my \$i = 0; \$i < @rows; \$i++) {
for (my \$j = 0; \$j < @{\$rows[\$i]}; \$j++) {
print(\$rows[\$i][\$j], "  ");
}
}
``````
0

Author Comment

ID: 21763025
ok you're right, thanks.
0

LVL 85

Expert Comment

ID: 21763984
It may be safer to itterate
for my \$i( \$[ .. \$#rows ){ for my \$j ( \$[ .. \$#{\$rows[\$i]} )
Or a simpler way is
print "@\$_\n" for @rows;
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Join & Write a Comment Already a member? Login.

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
###### Suggested Courses
Course of the Month8 days, 10 hours left to enroll

#### 595 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.