Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Count similar characters in a row

by toolic (Chancellor)
on Oct 06, 2010 at 15:23 UTC ( #863814=note: print w/ replies, xml ) Need Help??


in reply to Count similar characters in a row

use strict; use warnings; while (<DATA>) { my @cols = split; shift @cols; my $all_r = 1; for my $c (@cols) { if ($c ne 'r') { $all_r = 0; last; } } print if $all_r; } __DATA__ 0 a b h 1 - r z 3 u - u 4 r x r 5 r t r 6 r r r 7 r r r 8 r r r

prints out:

6 r r r 7 r r r 8 r r r

update: even better with List::MoreUtils:

use strict; use warnings; use List::MoreUtils qw(all); while (<DATA>) { my @cols = split; print if all { $_ eq 'r' } @cols[1 .. $#cols]; } __DATA__ 0 a b h 1 - r z 3 u - u 4 r x r 5 r t r 6 r r r 7 r r r 8 r r r


Comment on Re: Count similar characters in a row
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://863814]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2014-07-11 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (236 votes), past polls