in reply to PLEADE HELP ME!![NEWBIE]

I submit this because it was fun. It works for me, and is intentionally ugly:

use strict; use warnings; while(<DATA>){ my$max=0; s/$/, /; my$x=()=/,/g; for my$n(1..$x){ if(/^((?:\d+, ){$n})\1+$/){ $max=$n;last; } } print$/,($max>0?$x/$max:0); } __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

Replies are listed 'Best First'.
Re^2: PLEADE HELP ME!![NEWBIE]
by Anonymous Monk on Dec 21, 2011 at 16:54 UTC
    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!
      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/,//;

      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