in reply to Re: PLEADE HELP ME!![NEWBIE]
in thread PLEADE HELP ME!![NEWBIE]

Hello,
I am "another" Anonymous Monk and I would like to tie on here:
I see that this code works but I do not understand how. :-(
Could you please explain what does s/$/, /; and also my$x=()=/,/g; do?
Thanks!

Replies are listed 'Best First'.
Re^3: PLEADE HELP ME!![NEWBIE]
by runrig (Abbot) on Dec 21, 2011 at 17:02 UTC
    s/$/, /;
    "$" is the end of the string, so you are "substituting" ", " for the end of the string (i.e. appending ", " to the string). Which would be just as well (or better) written as:
    $_ .= ", ";
    Then:
    my$x=()=/,/g;
    First, /,/g will return an array of commas in array context. The "()" provides the array context. The my $x = gets the array in scalar context, which is the number of elements in the array. Since the regex consists of only one character, this could also be written (IMO) more simply as:
    my $x = tr/,//;
Re^3: PLEADE HELP ME!![NEWBIE]
by hbm (Hermit) on Dec 21, 2011 at 19:02 UTC

    First, as I hinted at originally, this not intended to be production-quality. In fact, it is just how I would start a golf problem - get it working, then trim away.

    runrig explained the how. The why (why add comma-space at end) is to make the whole string uniform (digit-comma-space, digit-comma-space, digit-comma-space, etc.) for easy matching.

    The fun hasn't worn off yet, so here's some uglier code:

    map{ $m=!s/$/, /; $x=y/,//; for$n(1..$x){/^((?:\d+, ){$n})\1+$/&&{$m=$n,last}} print$/,($m>0?$x/$m:0); }<DATA> __DATA__ 1, 2, 5, 1, 2, 5, 1, 2, 5 3, 6, 3, 6, 3, 6, 3, 6 4, 1, 28, 0, 4, 1, 28, 0 3, 5, 17, 3, 17, 5 4, 1, 28, 0, 4, 1, 28