i believe the order of the second row of your output is incorrect.

in order to output the husbands money respectively, the affected part of your regex should read:

(?: \1 (?: \2 \2 (??{ ".{" . 1.5*length($3) . "}" }) $ (?{ print join(" ", map .5 * length, $1 x 2, $3 x 3, $2 x 4), +"\n" }) | \3 \3 (??{ ".{" . 1.5*length($2) . "}" }) $ (?{ print join(" ", map .5 * length, $1 x 2, $2 x 3, $3 x 4), +"\n" }) ) | \2 (?: \1 \1 (??{ ".{" . 1.5*length($3) . "}" }) $ (?{ print join(" ", map .5 * length, $2 x 2, $3 x 3, $1 x 4), +"\n" }) | \3 \3 (??{ ".{" . 1.5*length($1) . "}" }) $ (?{ print join(" ", map .5 * length, $2 x 2, $1 x 3, $3 x 4), +"\n" }) ) | \3 (?: \1 \1 (??{ ".{" . 1.5*length($2) . "}" }) $ (?{ print join(" ", map .5 * length, $3 x 2, $2 x 3, $1 x 4), +"\n" }) | \2 \2 (??{ ".{" . 1.5*length($1) . "}" }) $ (?{ print join(" ", map .5 * length, $3 x 2, $1 x 3, $2 x 4), +"\n" }) ) )
the affected areas are in the print statements. the order of the multipliers (2, 4, 3) in your original code should be (2, 3, 4,) which will make the output:
1220 1320 1420 1220 1980 2840
which is, left to right, in format: names (multipliers)
first row: Camille, June, Martha
second row: Jack Smith (x100%), Horace Sanders (x150%), Terry Conners (x200%)

i must say, i'm still trying to wrap my brain around this code. it's quite a lot for me to process. in fact, i'm amazed to learn that these type of problems can be attacked by regular expressions. brilliant! now, if i can only understand how to do it, then i can use it.

as far as my mechanical engineering background allows me to understand your algorithm, this is what i've been able to figure out. you've created a vector of 10_000 '.' characters. your first regexes work within the first 3_960 elements to determine the answer to the equation:

Camille + June + Martha = Total wives = 3_960
or, after a bit of algebra:
C + ( C + 100 ) + ( C + 200 ) = 3 * C + 300 = 3_960

although i don't understand your advanced regex magic, the algorithm is quite clear. the length of the matches is printed, and the elements are discarded, leaving the remainder of the vector as the husbands' total.

enter the second stage of regex magic. i think you are basically using vector products in this bit of code. i can explain the print statement pretty well (the tricky bit is the map .5 * length, ... which translates the multipliers (2, 3, 4) into the problem multipliers of (100%, 150%, 200%).

what i don't understand is what seems to me to be superfulous code. i hope you can explain this to me. if i modify your second section of your regex to read:

(?: \1 (?: \2 \2 (??{ ".{" . 1.5*length($3) . "}" }) $ (?{ print join(" ", map .5 * length, $1 x 2, $3 x 3, $2 x 4), +"\n" }) | \3 \3 (??{ ".{" . 1.5*length($2) . "}" }) $ (?{ print join(" ", map .5 * length, $1 x 2, $2 x 3, $3 x 4), +"\n" }) ) )
(i've removed everything after the \1 block) the answer remains the same. what are the consequences of the removal of this code?

again, i've never seen this type of problem approached in this manner, and i'm awed by the power, beauty, and simplicity of a well-crafted solution using perl's regular expression engine. i know understanding your method (and madness) will make me a better coder, and an even bigger fan of Perl.

~Particle


In reply to Re: Re: Passing a complex array to a function by particle
in thread Passing a complex array to a function by Anonymous Monk

Title:
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, details, 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, summary, 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.