Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

extracting a list from a lists of lists

by tachekent (Acolyte)
on Jan 02, 2003 at 00:09 UTC ( #223650=perlquestion: print w/replies, xml ) Need Help??

tachekent has asked for the wisdom of the Perl Monks concerning the following question:

If I have a lists of lists, how do I copy one of the sub-lists into a new list?

I tried @newlist = $LoL[$n], but that always seems to evaluate $LoL$n in scalar context.


Replies are listed 'Best First'.
Re: extracting a list from a lists of lists
by gjb (Vicar) on Jan 02, 2003 at 00:20 UTC

    The list of list has references as elements, so writing the assignment as @newlist = @{$LoL[$n]}; will dereference this reference to a list.

    Have a look at perldata and perlfaq4.

    Hope this helps, -gjb-

Re: extracting a list from a lists of lists
by tadman (Prior) on Jan 02, 2003 at 01:08 UTC
    As gjb mentioned, you can copy with a dereference. Sometimes it's not necessary to copy, though, so you can always just use the reference as is. Here's some examples:
    my $ref = $LoL[$n]; my @ary = @{$LoL[$n]}; $ref->[0] = "Foo"; # Modifies @LoL directly $ary[0] = "Foo"; # Only modifies @ary copy foreach (@$ref) { ... } foreach (@ary) { ... }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://223650]
Approved by gjb
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2022-08-08 19:03 GMT
Find Nodes?
    Voting Booth?

    No recent polls found