user2828488
user2828488

Reputation: 65

For loop help in perl

I am writing perl script and I have little question regarding for loop limit.

Let say I have two arrays, arr1 has serial numbers and arr2 is two dimensional array, the first dimension is the serial number [same as arr1] and the second dimension is the contents of that serial number , Now I want to apply the for loop for this two dimension array but I am confused at the limit . Till now I have this code

Example : I have Three serial numbers , 1 ,2 ,3 . Serial 1 has 2 contents 1,5 . Serial 2 has 1 content i.e 1. Serial 3 has two contents  1,1.
@arr1 = (1,2,3) 
$arr2[0][0] = 1
$arr2[0][1] = 5
$arr2[1][0] = 1
$arr2[2][1] = 1
$arr2[2][2] = 1 
Note: As you can see the contents of arr2 has arr1 elements in 1st columns and the contents in the second columns.

for (my $i = 0; $i <= $#arr1; $i++) {
    print( "The First Serial number has:" );
    for (my $j = 0; $j <= $#arr2; $j++) {

        print( "$arr2[$i][$j]\n" );
    }
}

Thanks, Sorry for the bad explaination

Upvotes: 0

Views: 135

Answers (5)

sanooj
sanooj

Reputation: 493

Try this.

    my @arr2;
    $arr2[0][0] = 1;
    $arr2[0][1] = 5;
    $arr2[1][0] = 1;
    $arr2[2][0] = 1;  
    $arr2[2][1] = 1;


    foreach $inside_array (@arr2){
            foreach $ele (@$inside_array){
                    print $ele,"\n";
            }
    }

Its always better to use foreach instead of for/while, this will eliminate any possibility of bugs. Especially with judging proper condition to exit the loop.

Upvotes: 0

blio
blio

Reputation: 483

you can put @arr2 like this and it would be much easier for you to understand @arr2

use strict;
use warnings;

my @arr1 = (1, 2, 3);
my @arr2 = ([1, 5], [1], [1, 1]);
for my $first(@arr1) {
    for my $second (@{$arr2[$first-1]}) {
        print $second."\n";
    }
}

Upvotes: 1

DeVadder
DeVadder

Reputation: 1404

Here is a version without the first array.

for (my $i = 0; $i<= $#arr; $i++)
{
    print "INDEX $i\n";
    for (my $j = 0; $j <= $#{$arr[$i]}; $j++)
    {
        print "${arr[$i][$j]}\n";
    }
}

The point here is that a two dimensional array is in fact an array of arrays (well actually array references, but that does not change anything here). So in the inner loop, you should check against the size of the array that is stored in $arr[$i].

Upvotes: 0

ysth
ysth

Reputation: 98508

Fixed code:

use strict;
use warnings;
my @arr1 = (1,2,3);
my @arr2;
$arr2[0][0] = 1;
$arr2[0][1] = 5;
$arr2[1][0] = 1;
$arr2[2][0] = 1;  # original code had
$arr2[2][1] = 1;  # these indexes wrong

for (my $i = 0; $i <= $#arr1; $i++) {
    print( "Serial number $arr1[$i] has:" );
    for (my $j = 0; $j <= $#{ $arr2[$i] }; $j++) {
        print( "$arr2[$i][$j]\n" );
    }
}

Note the use of $#{ arrayref }; see http://perlmonks.org/?node=References+quick+reference

Upvotes: 1

gchap
gchap

Reputation: 11

Why don't do this like that :

#!/usr/bin/perl
use strict;

my @arr;
$arr[0][0] = 1;
$arr[0][1] = 5;
$arr[1][0] = 1;
$arr[2][1] = 1;
$arr[2][2] = 1;

my ($i, $j);
foreach $i (@arr) {
        foreach $j (@{$i}) {
                print $j."\n" if($j);
        }
}

1;
__END__

Upvotes: 1

Related Questions