I am guessing you want to get something like this.

User enters a list of food items Program looks up group for food items Program looks up rules to see if those groups can be combined or not. Program reports to user

The best aproach for this would be to make a hash with the food item as the key and its group number as the value stored under this key. No playing with refs there :(

Perhaps you want the user to enter a food item and the program will suggest possibly combinations to go with this, in this case the best approach is probably functional programing ! I think Haskell is the popular one round here. But that is not why we're here is it, to do it in perl you may well want to build an array or hash of arrays. As your food groups are numeric in your rules I'd be inclined to use array of arrays.

@food=( ['eggs', 'fish'], ['cheese', 'lard'], ['breed', 'rice'], ['beer', 'wine'], ); print $food[3][0]; # prints beer

If you want to do the hash of arrays you can build it up like this

%food=( 'carbs' => ['breed', 'rice'], 'tasty' => ['chocolate', 'pistacio'], ); print $food{tasty}[1]; # prints pistacio

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

In reply to Re: Continuing from "Turning foreach into map?" - perlreftut and References by Random_Walk
in thread Continuing from "Turning foreach into map?" - perlreftut and References by ghenry

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.