Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

PLEADE HELP ME!![NEWBIE]

by Anonymous Monk
on Dec 20, 2011 at 19:40 UTC ( [id://944456]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, i need to check an array for serial match: ex,
1, 2, 5, 1, 2, 5, 1, 2, 5 - will print 3 3, 6, 3, 6, 3, 6, 3, 6 - will print 4 4, 1, 28, 0, 4, 1, 28, 0 -will print 2 3, 5, 17, 3, 17, 5 - will print 0 4, 1, 28, 0, 4, 1, 28 - will print 0
i tried a few thing and nothing... ani ideas? Thanks

Replies are listed 'Best First'.
Re: PLEADE HELP ME!![NEWBIE]
by cavac (Parson) on Dec 20, 2011 at 20:07 UTC

    First of all, on future posts please use a title that actually describes your problem. Screaming, pleading for help and saying that you are a newbie are not very helpful in getting people with knowledge about the subject of your problem to read your post.

    Second, it's a good thing you posted the output of your script. But to help you, we also need to see the input of your script as well as the script itself.

    Third, a better description of what the problem is, what you expect as output as well as an example for the correct output would be very helpful.

    Just post your best try. A small, runable test example is what we are looking here. Something that for example i can just copy/paste into my debugger and have it run without fiddling around too much. That way, i wouldn't waste time to get your code to run but could actually spend my time hunting the problem down and proposing a solution.

    What you have to understand here is: We do this in our (unpaid) spare time. The more help you can give us, the more we are motivated to actually bother about your problem, dig into your code and help you get it fixed.

    Look at it this way: Every day there are more than 20 posts asking for help (probably peaking at a hundred on very busy days). Since we do this for our fun and education, many (most) of us pick the problems that are easy to replicate on our own computers. That is not to say we only pick problems that are easy to fix - we like a challenge. But that challenge should be in finding a solution to a well specified problem. Not in trying to find out what the one asking the question is trying to ask and failing to do so.

    On a side note: If you are not using strict and warnings, we are probably going to ask you to do so before touching your code. So, use strict and use warnings, and provide all the information mentioned above and you have a better change of quicker help.

    BREW /very/strong/coffee HTTP/1.1
    Host: goodmorning.example.com
    
    418 I'm a teapot
Re: PLEADE HELP ME!![NEWBIE]
by Anonymous Monk on Dec 20, 2011 at 20:24 UTC
    This works for me:
    @aray = ( '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', ); for ($i = 0; $i < @aray; $i++) { if ($aray[$i] eq '1, 2, 5, 1, 2, 5, 1, 2, 5') { print 3 } if ($aray[$i] eq '3, 6, 3, 6, 3, 6, 3, 6') { print 4 } if ($aray[$i] eq '4, 1, 28, 0, 4, 1, 28, 0') { print 2 } if ($aray[$i] eq '3, 5, 17, 3, 17, 5') { print 0 } if ($aray[$i] eq '4, 1, 28, 0, 4, 1, 28') { print 0 } }

      yuck!

      • "Array" is misspelled.
      • "Array" is a poor name for variable.
      • Needless comparisons are being done. At a minimum, use elsif, but a lookup table would be better!
      • C-style for loop is needlessly complex.
      • No line breaks output.
        • misspelled but consistent ... no undeclared variable were used (besides, did you see the OP?)
        • poor, but it works
        • again, it works
        • complex, but it works
        • there will be line breaks if you populate $/ with a newline
        • you really take yourself much too seriously (this was a joke dude!)
Re: PLEADE HELP ME!![NEWBIE]
by keszler (Priest) on Dec 20, 2011 at 19:52 UTC

    Show us what you tried, we'll try to show where it went wrong.

Re: PLEADE HELP ME!![NEWBIE]
by hbm (Hermit) on Dec 20, 2011 at 20:28 UTC

    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
      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
Re: PLEADE HELP ME!![NEWBIE]
by Eliya (Vicar) on Dec 20, 2011 at 21:50 UTC

    If you massage your lists into strings of a form similar to "1, 2, 5, 1, 2, 5, 1, 2, 5, ""3, 6, 3, 6, 3, 6, 3, 6, " etc., you can do

    $rep++ while $seq =~ s/(.+?)\K\1//;

    This successively removes a substring (.+?) followed by itself (\1), and counts (in $rep) how many times this can be done.

    Note that this counts the number of repetitions, not occurrences (like, in "abcabc", "abc" is repeated once). If you rather want the number of occurrences, just add one...

      $seq .= ', '; my $rep = 1; $rep++ while $seq =~ s/(.+?)\K\1//;
      doesn't work for 11, 2, 11, 2

        Thanks.  It can be fixed by being more specific in the match:

        $rep++ while $seq =~ s/^((?:[^,]+, )+?)\K\1//;
Re: Count occurrences of a pattern
by ikegami (Patriarch) on Dec 20, 2011 at 21:01 UTC
    /^(.*?)(?:, \1)*\z/; my $count = (length($_)+2)/(length($1)+2);

    Note that this outputs the correct number of occurrences (one) for the last two inputs, not zero as the OP stated.

Re: PLEADE HELP ME!![NEWBIE]
by JavaFan (Canon) on Dec 20, 2011 at 23:39 UTC
    use 5.10; my @lines = split /\s*\n/, <<"EOT"; 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 EOT foreach (@lines) { $_ .= ", "; say /^(.+?)\1+$/ ? length()/length($1) : 0; } __END__ 3 4 2 0 0
Re: PLEADE HELP ME!![NEWBIE]
by TJPride (Pilgrim) on Dec 21, 2011 at 06:21 UTC
    You didn't say whether you wanted just the -largest- number of consecutive matches, or all. And I'm only theorizing that you want to exclude instances where there are consecutive matches but they don't go all the way to the end of the string. Not enough information. But here:

    use strict; use warnings; my ($i, $j); while (<DATA>) { chomp; @_ = split /, /; OUTER: for $i (1..(($#_+1)/2)) { next if ($#_+1) % $i != 0; for ($j = $i; $j <= $#_; $j += $i) { next OUTER if "@_[0..($i-1)]" ne "@_[$j..($j+$i-1)]"; } $j = ($#_ + 1) / $i; print "@_ = $i of $j-chunks\n"; } } __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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-14 14:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found