Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Banging my head over this all day. Basically, I have a list of products (an arbitrary list stored in a database), which I pull out into an array called @products. Each product can have a number of attributes, as shown below. I made a simple loop to go through colors and sizes and print exactly what I'm look for in terms of output. Unfortunately, the number of attributes is also arbitrary, so there could be sizes, colors, styles, flair, etc. So I need to come up with a way to print the list of products with every available option dynamically. Here is the simple code that prints what I want.
#!/usr/bin/perl use strict; my @products = ('tshirt', 'pants', 'sweatshirt'); my @sizes = ('small', 'medium', 'large'), my @colors = ('red', 'green', 'blue'); foreach my $product (@products) { foreach my $size (@sizes) { foreach my $color (@colors) { print "$product $size $color\n"; } } }
Here is the output I want
tshirt small red
tshirt small green
tshirt small blue
tshirt medium red
tshirt medium green
tshirt medium blue
tshirt large red
tshirt large green
tshirt large blue
pants small red
pants small green
pants small blue
pants medium red
pants medium green
pants medium blue
pants large red
pants large green
pants large blue
sweatshirt small red
sweatshirt small green
sweatshirt small blue
sweatshirt medium red
sweatshirt medium green
sweatshirt medium blue
sweatshirt large red
sweatshirt large green
sweatshirt large blue
Here is an example (%options) of the structure I will be building dynamically from the database. I am simply stuck on how to get from this structure to the output listed above! Any help would be greatly appreciated. Please note that the hard part seems to be the fact that the %options has can grow and shrink!
my @products = ('tshirt', 'pants', 'sweatshirt'); my %options = ( 'sizes' => ['small', 'medium', 'large'], 'colors' => ['red', 'green', 'blue'] );

In reply to Conglomerate of arrays with no duplicates by saberworks

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (6)
    As of 2020-05-29 14:31 GMT
    Find Nodes?
      Voting Booth?
      If programming languages were movie genres, Perl would be:

      Results (169 votes). Check out past polls.