http://www.perlmonks.org?node_id=223650

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.

confused...

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) { ... }