BBQ has asked for the wisdom of the Perl Monks concerning the following question:
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 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 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 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 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.# 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; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Referencing Array Slices as Lists?
by takshaka (Friar) on Sep 05, 2000 at 12:10 UTC | |
Re: Referencing Array Slices as Lists?
by merlyn (Sage) on Sep 05, 2000 at 04:36 UTC | |
Re: Referencing Array Slices as Lists?
by agoth (Chaplain) on Sep 05, 2000 at 13:07 UTC |