Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Find duplicate digits

by inman (Curate)
on Feb 15, 2006 at 15:35 UTC ( #530415=note: print w/replies, xml ) Need Help??

in reply to Find duplicate digits

Canonicalise the data and look for multiples. The grep tests for numbers where there are a number of multiples of which at least one is a pair. This solution works with numbers of any length and can be made to work with triples, quads etc. just by changing the grep test.

The OP didn't make it clear whether a number such as 0101 that has two pairs should be counted. You can always test the number of members of the list returned by the grep.

while (<DATA>){ print "$_" if grep {length $_ == 2} join('', sort split //, $_) =~ + /((\d)\2+)/g; } __DATA__ 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 01511 451411
0011 0012 0013 0014 451411

Replies are listed 'Best First'.
Re^2: Find duplicate digits
by Anonymous Monk on Feb 15, 2006 at 15:52 UTC
    Thanks, inman!

    Yes, I would like 1010 to be counted.

    I'm finding it hard to understand your grep. Where is the list that it's supposed to work on?

      The code is equivalent to
      while (<DATA>) { my $a = join('', sort split //, $_); my @a = $a =~ /((\d)\2+)/g; print "$_" if grep {length $_ == 2} @a; }

      holli, /regexed monk/
        Thanks, holli! That really helpful :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://530415]
[talexb]: TStanley I'm flying to Vegas in two weeks time. After that flights leaving Vegas and Phoenix are late at night .. I've been hearing that it gets so hot that the air isn't dense enough for planes to take off. Weird. Guess you won't have that problem.
[Corion]: Yay, vacation time now - one week off work (but a Perl workshop in the middle ;)) )
[talexb]: TStanley I just realized that I was in your area for a convention back in April. We were at the high school for the weekend, the one with the long driveway. Nice spot.
[TStanley]: I am heading to Arlington, Virginia next week, then I get to spend an entire week at home. Looking forward to that as I will have spent 7 weeks on the road by the end of next week

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2017-06-23 14:19 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (548 votes). Check out past polls.