Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Getting individual lists from a bigger list

by vishi (Beadle)
on Jul 04, 2013 at 07:41 UTC ( #1042360=perlquestion: print w/replies, xml ) Need Help??
vishi has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I'm sure this may have been asked earlier but I searched quite a bit for this and am unable to find something that matches my requirement.

Ok Here is the deal - I have an empty list to start with, say @bigList, to which I keep pushing smaller lists - in a loop. Essentially, when the loop finishes, the @bigList list will now be a list of lists, like so:

@tempList = <obtained in another part of the code> <LOOP HERE> { push(@bigList, @tempList); }

Now, later on in the code, I want to retreive the individual lists that I pushed into @bigList, how do I do this? I read about dereferencing, and getting the reference of the array, but I end up getting the reference of @bigList and not the individual arrays.

Tried the following:



Replies are listed 'Best First'.
Re: Getting individual lists from a bigger list
by hdb (Prior) on Jul 04, 2013 at 07:48 UTC

    Are you sure the logic is right? You would be pushing the same @tempList onto @bigList in each iteration of the loop. I would expect it more like this:

    <LOOP HERE> { @tempList = <obtained in another part of the code> push(@bigList, @tempList); }

    Also if you push it this way, you do not get a list of lists, Perl will flatten everything into one big list. You need to push a reference to a list onto the @BigList. If you re-use @tempList, then you also need to copy the array, otherwise you overwrite previous results. So I would propose something like:

    push @BigList, [ @tempList ];

    which creates a reference to a copy of @tempList.

    UPDATE: To access this structure, you would say $BigList[2]->[5] to access the third small list's sixth element (counting starts at zero).

Re: Getting individual lists from a bigger list
by Loops (Curate) on Jul 04, 2013 at 07:57 UTC

    As hdb said, unless you push a reference the original list is lost and there is no way to recover. If you push a reference you don't have a flat list in @biglist though, you have a list of lists which may not be what you want. Maybe the below code will help make it clear

    use strict; use warnings; my @source = ( [1, 2, 3], [4, 5, 6], [7, 8, 9] ); my @bl1; print "BL1:\n"; push @bl1,@$_ for @source; print "$_\n" for @bl1; my @bl2; print "BL2:\n"; push @bl2,$_ for @source; print "@$_\n" for @bl2;


    BL1: 1 2 3 4 5 6 7 8 9 BL2: 1 2 3 4 5 6 7 8 9
Re: Getting individual lists from a bigger list
by poj (Prior) on Jul 04, 2013 at 10:13 UTC

    If you want to keep @bigList unchanged then you could use another array to keep start and end positions for each list added, something like this

    #!perl; use strict; use warnings; my @test = ([ qw(A B C) ],[ qw(1 2 3 4)], [qw( x y )]); my @bigList = (); my @chunk = (); for my $ar (@test){ my @templist = @$ar; my $start = scalar @bigList; push @bigList,@templist; my $end = $#bigList; push @chunk,[$start,$end]; } print "@bigList \n"; # each smaller list for my $ar (@chunk){ my ($start,$end) = @$ar; print "@bigList[$start..$end] \n"; }
Re: Getting individual lists from a bigger list
by sundialsvc4 (Abbot) on Jul 05, 2013 at 02:11 UTC

    You need to push a reference to the sublist onto the bigger list, taking care, as noted, that Perl does not “flatten” them.   (Data::Dumper is your best friend.)

    The reason why references are needed is that, in the end, all of these data structures are one-dimensional.   If @biglist is supposed to contain, say, 5 sublists, then it will then be a list containing 5 entries, each one of which is a reference to its own separate list.

    There are, as noted, several ways to build the final structure, and Data::Dumper is quite smart about printing out what it actually sees.   Create a tiny stand-alone test case to prove your logic.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1042360]
Approved by hdb
[stevieb]: Playing around with two DACs to control a single RGB LED (each DAC only has two outputs, LED has 3 inputs), and have realized that each of my IC RPi dists needs a wiring diagram, which is linked to within the documentation

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2017-08-20 19:29 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (317 votes). Check out past polls.