Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

variable number of lists

by jbfamilly (Initiate)
on Aug 17, 2009 at 15:14 UTC ( #789190=perlquestion: print w/replies, xml ) Need Help??
jbfamilly has asked for the wisdom of the Perl Monks concerning the following question:

How do I handle variable number of lists? I need to accept a variable number of lists and then step through all the lists at the same time. Simple example: User creates a file with:
fruit:apple fruit:orange fruit:banana veg:celery veg:cucumber veg:been
So I read this file into a hash of arrays but how do I go through the lists to get:
Day 1: apple celery Day 2: orange cucumber Day 3: banana been
The next time this is run there could be 3 things: fruit, veg, meat...

Replies are listed 'Best First'.
Re: variable number of lists
by moritz (Cardinal) on Aug 17, 2009 at 15:17 UTC
    You might either need an array of arrays or a mixture of arrays and hashes. See perldata for hashes and perlreftut for how to put arrays into hashes and the other way round.
      Missed an important point. I need to know which list the data is coming from. More accurate result would be:
      Day 1: fruit=apple veg=celery Day 2: fruit....

        Basically, you either know all the lists are the same length, or you extract the length of the longest one programmatically (you say you know how to make the data structure so I assume you know how to access it). Once you have that information, do a foreach $element_num (1..$foo) { where $foo is your number of elements in the longest list. For each number, you extract one element from each list with an internal foreach loop that uses keys to get you the individual list names.

        Alternately, you could make an array of arrays and just make element 0 of each row the name of that list--then it should be fairly obvious how to march across all the rows in lock-step. It depends on whether you want easy access to the data by list name.

        For example by iterating over the indexes of one array, and accessing the the items "simultaneously" (whatever that means if you don't use threads...) by index.
Re: variable number of lists
by ack (Deacon) on Aug 17, 2009 at 16:54 UTC

    Here's one way to do it using a hash of arrays (HoAs). It is not necessarily the simplest or most efficient; but I think it illustrates one strategy that is in the vein of the various responses to your question. Similar strategies could work with Hash of Hashes (HOHs), Array of Arrays (AoAs).

    The code produces the output:

    Day 1: fruit=>apple, veg=>celery Day 2: fruit=>orange, veg=>cucumber Day 3: fruit=>banana, veg=>bean

    which I believe is the output that you're looking for.

    The only proviso is that the number of entries for each type of food must equal the number of days to be populated. This could be handled alternatively (and without that restriction) by simply looking first for the type of food with the least number of entries and then restricting the number of days to be populated to that number of days. Or, of course, you could presumably work out a strategy that re-cycles through types of foods that have insufficient numbers of entries.

    My offered solution does not handle those cases; but I don't think, IMHO, it is particularly hard to add that capability.

    Note that I put the input data into a hash in the code. In your case you'd want to populate the hash from the input file that your user created. I presume you would know how to do that (if not, it's a good exercise for you).

    Hope this helps and gives you some ideas in addition to all the great advice from the other Monks.

    ack Albuquerque, NM
Re: variable number of lists
by dwm042 (Priest) on Aug 17, 2009 at 18:43 UTC
    Not to sound too off the beaten path, but if you were to use the module Config::Simple and its http syntax, an awful lot of the work will be done for you.

    I'd also like to know where you're getting the "Day" field from. Is that a date the file was created? Is that a date embedded in the file the user creates? Expanding on that would help us all point to a solution.

      I thought to keep things simple and use a silly example. Seems to have had the opposite effect and made things complex.
      Leason learned.

      Anywhoo, the brain fart has cleared and using the hash keys and number of elements did the trick.
      Thanks for the help!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://789190]
Approved by AnomalousMonk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2017-12-15 06:44 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (418 votes). Check out past polls.