Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

array slice?

by zeltus (Sexton)
on Jan 31, 2014 at 14:18 UTC ( #1072850=perlquestion: print w/ replies, xml ) Need Help??
zeltus has asked for the wisdom of the Perl Monks concerning the following question:

I think my question is to do with array slices... but of course, I may be wrong!

my @tarray = qw(one two three four five); my ($v2) = @tarray; my $v3 = (@tarray)[0];

Now, I am used to extracting slices using

my $v3 = (@tarray)[0];

in this case, extract the first element from @tarray and "give" it to $v3...but upon examining some legacy code, I came across...

my ($v2) = @tarray;

...which does the same thing. But I can't work out why it works. Can someone please explain? Is it classifiable as a form or array slicing? I prefer my way for clarity, but are there any pros and cons for either method?

Thank you!

Comment on array slice?
Select or Download Code
Re: array slice?
by zeltus (Sexton) on Jan 31, 2014 at 14:33 UTC

    aah, I begin to see. After a lookaround, I found some info in perlsub: I see my question is a variant of

    my($key, $value) = @_;

    (copy a subroutines' parameters into named variables)

    I guess this is clear enough... my fud was because my legacy code was dealing with just one parameter, making the code look a bit strange to me, is all

Re: array slice?
by Athanasius (Monsignor) on Jan 31, 2014 at 15:32 UTC

      Thank you!

      Amongst other things, this is me trying to reduce the problem to the simplest demo code and losing the plot ever so slightly!

      Reductio ad absurdum or somesuch... but no matter, I no where I am now

      Bill

Re: array slice?
by kcott (Abbot) on Feb 01, 2014 at 01:13 UTC

    G'day zeltus,

    I see ++Athanasius has provided feedback on the code you posted.

    In addition to the FAQ links he posted, you might also like to look at perldata: Slices.

    -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1072850]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2014-09-21 18:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (173 votes), past polls