On Sun, Feb 26, 2012 at 09:30:44PM -0500, Steve Bertrand wrote:
> I came across a question early this morning on a forum that
> intrigued me. I literally spent about five hours trying everything
> to solve it, but I couldn't.

Sometimes Perl is just a means to get at the corect tool:

my $attributes =
[
  { type => 'colors',  values => [qw/red green blue/]  },
  { type => 'sizes',   values => [qw/small large/] },
  { type => 'shades',  values => [qw/light dark/] },
];

local $" = ",";
say y/-/ /r for glob join "-", map "{@{$_->{values}}}", @$attributes;


red small light
red small dark
red large light
red large dark
green small light
green small dark
green large light
green large dark
blue small light
blue small dark
blue large light
blue large dark


or perhaps you like commas but dislike maintenance programmers:

my $attributes =
[
  { type => 'colors',  values => [qw/red green blue/]  },
  { type => 'sizes',   values => [qw/small large/] },
  { type => 'shades',  values => [qw/light dark/] },
];

$"=",";say for(glob(qq{@{[map+qq{{@{$_->{values}}}},@$attributes]}}))


red,small,light
red,small,dark
red,large,light
red,large,dark
green,small,light
green,small,dark
green,large,light
green,large,dark
blue,small,light
blue,small,dark
blue,large,light
blue,large,dark


The key here is using globbing.  See "perldoc -f glob" for the details.  Using
bsd_glob would clean up the (first) code a little.

> I know this isn't a beginner's question

None of the concepts in this code are particularly tricky, but there are
quite a few of them in a short piece of code.

>                Is there a way to simplify this within Perl?

I suppose the question of whether or not this is simplified comes down to
definitions.

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to