LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl: $data[$i]->[$j] problem (https://www.linuxquestions.org/questions/programming-9/perl-%24data%5B%24i%5D-%5B%24j%5D-problem-442884/)

PB0711 05-08-2006 04:17 PM

Perl: $data[$i]->[$j] problem
 
Hey,
Thank you in advance.

So here is my code
Code:

my $mat ='Matrix.txt';
open MAT, "$mat" or die "Problem opening file, $!\n";
my $huge;
my @listrow;
while (my $line = <MAT>){
        $huge .= $line;
        chomp($line);
        my @one_row=split(/\t/, $line);
        #print "This is 1 row@one_row \n";
        push @listrow, \@one_row;
}
my @dim=&DIM($huge); #so col in 0 row in 1
#print $listrow[0]->[3],"\n";
for (my $j=0;$j<=$dim[1];$j++){
        for(my $i=0;$i<=$dim[0];$i++){
                print "i ->$i\tj ->$j\n";
                print " dim is -> @dim\n";
                print $listrow[$j]->[$i],"\t";
        }
        print "\n";
}
sub DIM {
        my $line = $_[0];
        push my @row, (split /\n/, $line);
        my @arr;
        for (my $t=0; $t < @row; $t++){
                push (@arr, split (/\t/, $row[$t]));
        }
        my @dim = @arr/@row;
        $dim[1]=@row;
        return @dim;# so col in 0 row in 1
}

So my two questions are this why does the for loop $listrow print out
"Use of uninitialized value in print at test.pl line 23."
and is there a better way to find the dimentions.
I know that I can print the matrix using the
Code:

$data->[$i][$j]
way but I don't want to double up on my for loops.

Cheers

Paul

PB0711 05-08-2006 06:06 PM

ok so I looked at it again and it was the for loop. But I would still like some help on finding a better way to get the dimentions of the array. I've probably killed my thread due to my reply taking it off the 0 reply list. :(

DanTaylor 05-08-2006 11:01 PM

Did you mean to assign $j and $t to 0 during each for loop? you probably meant to use ==

PB0711 05-09-2006 12:11 AM

Yea so what it was, was that the dimentions were off and it was tring to go for too long. So == probably would have worked as well. Thank you for the help.


All times are GMT -5. The time now is 12:30 AM.