Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Getting individual lists from a bigger list

by vishi (Sexton)
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:

  • http://perldoc.perl.org/perllol.html
  • http://perldoc.perl.org/perlref.html
  • http://perldoc.perl.org/perldsc.html#ARRAYS-OF-ARRAYS

Help!

Comment on Getting individual lists from a bigger list
Download Code
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 (Chaplain) 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;

    Output:

    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 (Priest) 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"; }
    poj
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2014-10-23 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (123 votes), past polls