Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Two dimensional array problem

by coec (Chaplain)
on Jul 21, 2002 at 03:14 UTC ( [id://183737]=perlquestion: print w/replies, xml ) Need Help??

coec has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I want to set up a two dimensional array hold start and stop positions. There are 8 rows and two columns (although there may be more rows in future). I have defined my array as:
my @Column_Info = { [ 1024, 1067 ], # Column 1 start stop [ 1068, 1093 ], # Column 2 start stop [ 1094, 1137 ], # Column 3 start stop [ 1138, 1163 ], # Column 4 start stop [ 1164, 1207 ], # Column 5 start stop [ 1208, 1233 ], # Column 6 start stop [ 1234, 1277 ], # Column 7 start stop [ 1278, 1321 ] }; # Column 8 start stop
When I try and use the array
my $StartPos = $Column_Info{$Count->[0]}; # Slot to start at my $Pos = $StartPos; # Slot to start at my $EndPos = $Column_Info{$Count->[1]}; open(IN,"cat /tmp/show_library.$$|"); while (<IN>) { $_ =~ s/^ //; while ($Pos <= $EndPos) { if ($Pos == 1024) { <snip>
I get the error
Use of uninitialized value in numeric le (<=) at ./show_library.pl lin +e 143, <IN> line 69. Use of uninitialized value in printf at ./show_library.pl line 140, <I +N> line 70.
How do I correctly set up and reference this array?
TIA
coec

PS: I have another question about returning a single dimension array from a subroutine but that can wait until later...

Replies are listed 'Best First'.
Re: Two dimensional array problem
by dpuu (Chaplain) on Jul 21, 2002 at 03:32 UTC
    I think your problems stem from your use of curly brackets when you define your array. @column_info is a list containing exactly one element -- a reference to a rather strange hash. To see what you're doing wrong, try adding:
    use Data::Dumper; print Dumper(\@Column_Info);
    If you get rid of the curlies (use round parentheses instead), then you'll be able to reference the array as:
    my @array = ( [0,0], [0,1], [1,0], [1,1] ); for (my $x=0; $x<4; $x++) { for (my $y=0; $y<2; $y++) { printf "%d,%d = %n", $x, $y, $array[$x][$y]; } }
Re: Two dimensional array problem
by Nitrox (Chaplain) on Jul 21, 2002 at 03:39 UTC
    Follow the advice in the previous post regarding the curly brackets and then change your reference's to:

    my $StartPos = $Column_Info[$Count][0];

    -Nitrox

Re: Two dimensional array problem
by flocto (Pilgrim) on Jul 21, 2002 at 09:30 UTC

    > How do I correctly set up and reference this array?

    If I was you I'd rather use an array reference. There is no need to do this, but it feels more appropriate. Your array could look like this:

    my $array_ref = [ [ 123, 456 ], [ 234, 567 ], [ 345, 678 ] ];

    This array reference can be used in a variety of ways, but the most obvious are these:

    my $pos = $array_ref->[$x][$y]; my $row = $array_ref->[$x]; my $column = $row->[$y];

    Regards,
    -octo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://183737]
Approved by ignatz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-24 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found