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

Re: Multiple Permutation handling

by Kenosis (Priest)
on Mar 01, 2013 at 21:00 UTC ( #1021339=note: print w/replies, xml ) Need Help??

in reply to Multiple Permutation handling

Here's a glob option:

use strict; use warnings; my $sku = join ',', qw/SKU1 SKU2 SKU3/; my $tslh = join ',', qw/T S L H/; my $size = join ',', qw/S M L XL 2X /; my $color = join ',', qw/BLU GRN WHT BLK/; print "$_\n" for glob "{$sku}{$tslh}{$size}{$color}";

Partial output:


Update:Removed unnecessary dashes in the string and the substitution. Thanks, kcott.

Replies are listed 'Best First'.
Re^2: Multiple Permutation handling
by rjt (Deacon) on Mar 01, 2013 at 21:35 UTC

    ++ for clever use of glob. It actually performs quite well in this case:

    Rate Algorithm::Loops glob Nested l +oops Algorithm::Loops 371/s -- -88% +-91% glob 3059/s 724% -- +-27% Nested loops 4219/s 1036% 38% + --
Re^2: Multiple Permutation handling
by Laurent_R (Canon) on Mar 02, 2013 at 22:20 UTC

    Hello dear Perl Monks,

    On a cross-post of the same question by the OP on the DevShed forum, I answered with a suggestion almost identical to Toolic's nested foreach loops.

    However, this use of the glob function puzzles me. After some tests and reading in detail the glob description, I found this passage: "If non-empty braces are the only wildcard characters used in the glob, no filenames are matched, but potentially many strings are returned. For example, this produces nine strings, one for each pairing of fruits and colors".

    So, now I understand why glob is being used in this context, but can anyone tell me why glob is doing this sort of things, which seems to have little to do with the normal usage of glob (by "normal", I just mean the usage that I have been knowing for years, i.e. returning filename extensions or a list of filename matching a shell file pattern)?

      In some shells, brace expansion is part of the filename patterns.
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Thanks, I get the point now.
Re^2: Multiple Permutation handling
by kcott (Chancellor) on Mar 03, 2013 at 12:32 UTC

    I was wondering why you added hyphens to the glob string and then added a map function to remove them. It seems like a lot of additional processing that has no real value: perhaps I'm missing something.

    This code produces the same output that you posted:

    use strict; use warnings; my $sku = join ',', qw/SKU1 SKU2 SKU3/; my $tslh = join ',', qw/T S L H/; my $size = join ',', qw/S M L XL 2X /; my $color = join ',', qw/BLU GRN WHT BLK/; print "$_\n" for glob "{$sku}{$tslh}{$size}{$color}";

    -- Ken

      ...perhaps I'm missing something.

      No, you haven't missed anything. I did unnecessarily add elements and processes. Good catch! ++

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1021339]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2018-05-28 03:55 GMT
Find Nodes?
    Voting Booth?