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

#!/user/bin/perl use strict; use warnings; my %inventory = ('fruit'=>['apple','orange','banana'], 'veg' =>['celery','cucumber','bean'], ); my $num_days = scalar(@{$inventory{'fruit'}}); foreach my $day (1..$num_days){ my @menu = (); foreach my $food (keys %inventory){ push(@menu,"$food=>$inventory{$food}->[($day-1)]"); } print "Day $day: " . join(", ",@menu) . "\n"; } exit(0);

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

In reply to Re: variable number of lists by ack
in thread variable number of lists by jbfamilly

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.