Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
The first issue here is what is "poo". Let's assign "poo" to a variable before we try to call a sub named "poo_sub".
#!/usr/bin/perl -w use strict; use Data::Dumper; my @poo = ( [1..4], ["goo", "doo", "moo"], [6..9] ); print Dumper (\@poo); #here the Dumper module is used (it is VERY handy) to #show the structure of @poo. __END__ $VAR1 = [ [ 1, 2, 3, 4 ], [ 'goo', 'doo', 'moo' ], [ 6, 7, 8, 9 ] ];
The [....] are anonymous array references. A list is well, a list of things. In this case @poo is a list of anon ref's to other lists. There is some distinction between list and an array, but in general this is just Perl internals and for the purpose of understanding what a "@var" is, makes no difference.

So let's call a subroutine with the list of 3 things in @poo:

#!/usr/bin/perl -w use strict; use Data::Dumper; my @poo = ( [1..4], ["goo", "doo", "moo"], [6..9] ); #first we just have 3 things, just pass 'em! 3 things in the call! poo (@poo); sub poo { my ($lref1, $lref2, $lref3) = (my @lrefs) = @_; #yes can do this, here we don't need individual ref's #just shown to demo that you can do that... foreach (@lrefs) { dump_lref ($_); } } sub dump_lref { my $lref = shift; #same as $lref = @_; here print "@$lref\n"; } __END__ prints: 1 2 3 4 goo doo moo 6 7 8 9
Now let's say that we don't want to pass the 3 things in the @poo list, we just want to pass a reference to the @poo list:
#!/usr/bin/perl -w use strict; use Data::Dumper; my @poo = ( [1..4], ["goo", "doo", "moo"], [6..9] ); #now just pass a ref to the @poo list ..... poo (\@poo); sub poo { my $ref_of_ref_to_list = shift; foreach my $lref (@$ref_of_ref_to_list) #@does one level of de-ref { dump_lref($lref); } } sub dump_lref ###WHOA same sub we used before! { my $lref = shift; #same as $lref = @_; here print "@$lref\n"; } __END__ prints: 1 2 3 4 goo doo moo 6 7 8 9
WHOA! same printout!

I think that I should mention that unlike some other languages, @list, $list, %list, sub list{} are all different things.


In reply to Re^3: Dereferenced Arrays by Marshall
in thread Dereferenced Arrays by SirDonkeyPunch

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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-04-20 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found