in reply to Keeping track of Pairs

This is the best I could come up with. This assumes you've already sorted the cards.

#!/usr/bin/perl -w use strict; my $hand = "33447"; # Check the hand for each card foreach my $card (2..10, "J", "Q", "K", "A") { # Count the number of cards in hand, by substituting each with its +elf # and getting the number of substitutions my $numCards = $hand =~ s/$card/$card/g; # Substitute the groups of similar cards with their quantity $hand =~ s/$card+/$numCards/g; } print $hand;

There is probably a more compact solution out there. :)


Replies are listed 'Best First'.
Re: Re: Keeping track of Pairs
by Albannach (Prior) on Jul 12, 2001 at 08:59 UTC
    How about this to replace your loop (though destroying the hand may not be a good idea in the long run):

    $hand =~ s/((.)\2*)/length $1/ge

    Update: Removed useless brackets

    I'd like to be able to assign to an luser