Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

2 dimensional array question

by redss (Monk)
on Sep 05, 2005 at 13:40 UTC ( #489224=perlquestion: print w/replies, xml ) Need Help??
redss has asked for the wisdom of the Perl Monks concerning the following question:

I want to use a 2 dimensional array in a manner that is easy to initialize, but am unable to get it to work. here is what I'm attempting:
@arr[0] = (1,2,3); @arr[1] = (4,5,6); $index=1; print "$arr[$index][2]";
Any idea why this doesn't work? I've also tried copying the array to another array to no avail, for example  @anotherarray=@arr[1]

Replies are listed 'Best First'.
Re: 2 dimensional array question
by reasonablekeith (Deacon) on Sep 05, 2005 at 13:58 UTC
    you need square brackets...
    $arr[0] = [1,2,3]; $arr[1] = [4,5,6]; $index=1; print "$arr[$index][2]";
    The square bracket returns a reference to an anonymous array. Your example is just a list, which is forced to scalar context (you're putting it into the first element of your array) which just returns the last element in your list. You can prove this by printing $arr[0];

    You might also have noticed I changed the @'s to $'s, which it prefered when referencing a single element of an array...

    my @array = (1,2,3); my $array[0] = 4;
    If you run your program with warnings on, it will tell you this.

    references are a big subject, you might want to check out ...

    perldoc perlref
    or "Learing/Programming Perl" which are excellent books.
    my name's not Keith, and I'm not reasonable.
      ah yes, the square brackets did the trick. thanks!
Re: 2 dimensional array question
by Roger (Parson) on Sep 05, 2005 at 14:13 UTC
    Note that your assignment is using array slices @arr[0] and @arr[1]. Although it works, you should use $arr[0] and $arr[1] instead because they are not the same thing.

    You could use the following method to initialize the 2-d array (use qw // and no commas, I hate to type commas :-)
    @arr = ( [ qw/ 1 2 3 / ], [ qw/ 4 5 6 / ] );

    Note that [ ... ] builds a reference to an array, it has 0 dimension. While ( ... ) builds an array, which has 1 dimension. When you assign an array to a zero dimension element @arr[0] or $arr[0], it only picks the first element from your array.

    And also you could use Data::Dumper to inspect your array afterwards.
    use Data::Dumper; @arr = ( [ qw/ 1 2 3 / ], [ qw/ 4 5 6 / ] ); print Dumper(\@arr); # and in your case @arr[0] = qw/ 1 2 3 /; @arr[1] = qw/ 4 5 6 /; print Dumper(\@arr);
Re: 2 dimensional array question
by ysth (Canon) on Sep 05, 2005 at 14:26 UTC
    Arrays elements and hash values can only be scalars, not hashes or arrays. When something is called an array of arrays or a two-dimensional array, this is short for array of references to arrays. This means they are assigned like:
    $arr[0] = [ 1, 2, 3 ];
    To copy the referred to array, you'd do
    @anotherarray = @{ $arr[1] };
      ah, thats what I was doing wrong... thanks!
Re: 2 dimensional array question
by blazar (Canon) on Sep 05, 2005 at 14:06 UTC
    How 'bout
    my @arr=([1,2,3], [4,5,6]); # IIUC
Re: 2 dimensional array question
by GrandFather (Sage) on Sep 05, 2005 at 19:06 UTC

    For another way to do it:

    use strict; use warnings; use Data::Dump qw(dump); my @arr; push @arr, [1,2,3]; push @arr, [4,5,6]; print dump (@arr); prints: ([1, 2, 3], [4, 5, 6])

    It is strongly recommended that you put "use strict; use warnings;" at the start of every bit of code you write. In this case the compiler would have told you "Scalar value @arr1 better written as $arr1" which is a clue that something is wrong, but might not tell you exactly what in this case.

    Note also the use of print dump (@arr) to dump the contents of the array. Try this code fragment using "()" in place of the "[]" and see what happens to the contents of the array.

    Perl is Huffman encoded by design.
Re: 2 dimensional array question
by trammell (Priest) on Sep 05, 2005 at 16:31 UTC
Re: 2 dimensional array question
by planetscape (Chancellor) on Sep 06, 2005 at 10:54 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://489224]
Approved by Arunbear
[stevieb]: w00t! my i2c module actually works, and it works well!

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2017-06-22 22:49 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (531 votes). Check out past polls.