Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: Multi-Dimensional Arrays and Array References

by Leudwinus (Scribe)
on Nov 16, 2020 at 17:59 UTC ( [id://11123702]=note: print w/replies, xml ) Need Help??


in reply to Re: Multi-Dimensional Arrays and Array References
in thread Multi-Dimensional Arrays and Array References

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

Replies are listed 'Best First'.
Re^3: Multi-Dimensional Arrays and Array References
by hippo (Bishop) on Nov 16, 2020 at 18:49 UTC
    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.


    🦛

Re^3: Multi-Dimensional Arrays and Array References
by Leudwinus (Scribe) on Nov 16, 2020 at 18:11 UTC

    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:  <%-{-{-{-<

        Thank you for the confirmation and thank you VERY much for that excellent, concise and clarifying example!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (1)
As of 2024-04-25 01:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found