Solved

Getting confused with 2d arrays

Posted on 2008-06-11
6
143 Views
Last Modified: 2011-09-20
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
Comment
Question by:DJ_AM_Juicebox
6 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 21761331

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

Open in new window

0
 
LVL 13

Expert Comment

by:marchent
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

by:DJ_AM_Juicebox
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 39

Accepted Solution

by:
Adam314 earned 500 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], "  ");  
    }
}

Open in new window

0
 

Author Comment

by:DJ_AM_Juicebox
ID: 21763025
ok you're right, thanks.
0
 
LVL 84

Expert Comment

by:ozo
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

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

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

Join & Ask a Question