Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??



Generate dynamically a string of the above code with all nesting and eval it. (fast and intuitive!)

2. Recursion

Write a function X2() which gets two arrayrefs and returns an arr_ref of the cross product. Call it recursively until @array exceeded.

3. Reduce

is a variation of the above, with List::Util::reduce

use Data::Dumper; #- crossproduct of two array refs sub X2 { my ($a,$b)=@_; my @result; for my $x (@$a) { for my $y (@$b) { unless (ref($x) eq "ARRAY"){ push @result, [$x,$y]; }else{ push @result, [(@$x,$y)]; } } } return \@result; } use List::Util qw/reduce/; #- crossproduct of list of array refs sub X { reduce { X2($a,$b) } @_ } my @array = ( [ "a", "b", "c", ], [ "1", "2", "3", "4", ], [ "x", "y", ], ); print Dumper X(@array);

The function X() now works somehow like the X operator in perl6, but of course you could also use reduce { X2($a,$b) } @array directly.

The function X2() could be rewritten with two nested maps, but thats a little two cryptic for my taste.

Bad ideas:

1. Glob

that's a hack which only works for strings as element type, everything else will be stringified, eg refs!!!

Cheers Rolf


* just noticed that you only want a simple concatenation of strings. That simplifies the code...

* There is a problem with this code .. the first one to spot it gets upvoted! :)

In reply to Re: Generating all possible combinations from an AoA by LanX
in thread Generating all possible combinations from an AoA by Anonymous Monk

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
    [Cosmic37]: I try to match successive date times stored in variable $mydt
    [Cosmic37]: I guess it is searching for the string "$mydt"
    [Corion]: Indeed cool, erix ;)
    [Cosmic37]: rather than the value of $mydt which is a date time strong such as 2016-01-01 12:30:56
    [Corion]: Cosmic37: No, but maybe $mydt doesn't contain what you think it does, or it contains characters that are special in a regular expression? Try if( $line =~ /\Q$mydt\E/) { ... for a literal match
    [Cosmic37]: I mean string grrr
    [Corion]: Maybe add an else branch in which you print what the values of $line and $mydt are?
    [Cosmic37]: ah thank you I will try
    [Cosmic37]: you are right $mydt did not contain what I expected... :-D I will now think :-D
    [Cosmic37]: crunch...grind... whirr...gnashing metal crunching cogs...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (9)
    As of 2017-06-29 17:02 GMT
    Find Nodes?
      Voting Booth?
      How many monitors do you use while coding?

      Results (673 votes). Check out past polls.