Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Multiple Permutation handling

by Kenosis (Priest)
on Mar 01, 2013 at 21:00 UTC ( [id://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:

SKU1TSBLU SKU1TSGRN SKU1TSWHT SKU1TSBLK SKU1TMBLU SKU1TMGRN ... SKU3HXLWHT SKU3HXLBLK SKU3H2XBLU SKU3H2XGRN SKU3H2XWHT SKU3H2XBLK

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

Replies are listed 'Best First'.
Re^2: Multiple Permutation handling
by rjt (Curate) 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 (Archbishop) 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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1021339]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-19 12:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found