I have been toying with references for a while now, and I bumped into something interesting. When you pass a reference to an array slice, you are not passing the list reference, but actually the references to each one of the elements in the list (please correct me if this is an incorrect statement).
To prove this, I came up with the following:
# example 1
my @ary = qw(
Foo and Bar
went up the Baz
to fetch a Gom Jabar
);
printref(\@ary[0,2,6,10,11]);
sub printref {
print "$$_\n" for @_;
}
And it works... No problem. The I thought of doing it the other way around. First pass the array ref, and then the parameters of what you want from the array. And we have:
# example 2
my @ary = qw(
Foo and Bar
went up the Baz
to fetch a Gom Jabar
);
printref(\@ary,(0,2,6,10,11));
sub printref {
my $ary = shift;
print "$$ary[$_]\n" for @_;
}
And again it works! Probably less efficient than the first, but still it works, no problem. Now here for the big trick that I wanted to pull off at first and obviously didn't get it to work:
# example 3
my @ary = qw(
Foo and Bar
went up the Baz
to fetch a Gom Jabar
);
printref(\@ary[0,2,6,10,11]);
sub printref {
my $ary = shift;
print "$_\n" for @$ary;
}
And we get
Not an ARRAY reference at - line 13.. So after all this running around, my question is, "
Is there a way to pass a reference of an array slice as a list?" Am I day dreaming again? Am I not understanding a basic concept? Any pointers in the right direction would be very appreciated.
#!/home/bbq/bin/perl
# Trust no1!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.