Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Multi-Dimensional Arrays and Array References

by choroba (Cardinal)
on Nov 16, 2020 at 16:01 UTC ( [id://11123697]=note: print w/replies, xml ) Need Help??


in reply to Multi-Dimensional Arrays and Array References

#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use Data::Dumper; my $ref = [[1, 2, 3], [4, 5, 6]]; # Print an element. say $ref->[1][1]; # Add a row. push @$ref, [7, 8, 9]; # Add a column. my @column = (3.5, 6.5, 9.5); push @{ $ref->[$_] }, $column[$_] for 0 .. $#column; # The same with an array. my @array = ([1, 2, 3], [4, 5, 6]); # Print an element. say $array[1][1]; # Add a row. push @array, [7, 8, 9]; # Add a column. my $column = \@column; push @{ $array[$_] }, $column->[$_] for 0 .. $#$column; Dumper($ref) eq Dumper(\@array) or die "Different";
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Multi-Dimensional Arrays and Array References
by Leudwinus (Scribe) on Nov 16, 2020 at 17:59 UTC

    Hi choroba,

    Thank you very much for your reply. If I may trouble you a bit more, I have a few more questions:

    1. When should I store my data in a reference such as

    my $ref = [[1, 2, 3], [4, 5, 6]];

    versus an array

    my @array = ([1, 2, 3], [4, 5, 6]);

    What is the difference?

    2. In both your first example using the reference, how come $ref->[$_] can’t be written as $ref[$_]?

    Gratias tibi ago
    Leudwinus

      When should I store my data in a reference ... versus an array

      In general consider using a reference when you plan to do 1 or more of these:

      • Passing your array to/from subroutines
      • Combining your array into a larger structure (see perldsc)
      • Using it as an object (unlikely, but not unheard of)
      • Having multiple references to the same data (obviously)

      See also the References tutorials subsection for other good info.


      🦛

      Regarding my second question above:

      Is it because $ref points to a reference of an array and therefore $ref->[2] would be the third element of that referenced array? Whereas $ref[2] would be the third element of the array @ref which perhaps doesn’t exist in this example?

        ... how come $ref->[$_] can’t be written as $ref[$_]?
        Is it because $ref points to a reference of an array .... Whereas $ref[2] would be the third element of the array @ref ...?

        Exactly. In Perl, @foo and $foo (and likewise %foo and etc.) are distinct variables which may have the same identifier, i.e., name. (Update: They are distinguished by their $ @ % sigils.) E.g.:

        Win8 Strawberry 5.8.9.5 (32) Mon 11/16/2020 14:26:26 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings -l use Data::Dump qw(dd); my $foo = [ 99, 42, 137, ]; dd '$foo', $foo; my @foo = (qw(foo bar baz)); dd '@foo', \@foo; my %foo = (qw(cero zero uno one dos two)); dd '%foo', \%foo; print $foo->[2]; print $foo[2]; print $foo{'dos'}; ^Z ("\$foo", [99, 42, 137]) ("\@foo", ["foo", "bar", "baz"]) ("%foo", { cero => "zero", dos => "two", uno => "one" }) 137 baz two
        (BTW: $ref does not point to a reference to an array, it is a reference to an array. :)


        Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-03-29 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found