I have been playing around with a script to figure out the point values of cribbage hands. This is the sub that I have come up with to count the number of fifteens (actually the points 2/15) I also have subs for pairs, and runs (I'll Tackle them later)
#!/usr/bin/perl -w use strict; use warnings; sub fifteens { my $hand = shift; $hand =~ s/[SCHD]//g; $hand =~ s/[JQK]/10/g; $hand =~ s/A/1/g; my ( $c1, $c2, $c3, $c4, $c5 ) = split /,/, $hand; my $total = 0; # count the 2 card combinations that add up to 15 $total += 2 if ( ( $c1 + $c2 ) == 15 ); $total += 2 if ( ( $c1 + $c3 ) == 15 ); $total += 2 if ( ( $c1 + $c4 ) == 15 ); $total += 2 if ( ( $c1 + $c5 ) == 15 ); $total += 2 if ( ( $c2 + $c3 ) == 15 ); $total += 2 if ( ( $c2 + $c4 ) == 15 ); $total += 2 if ( ( $c2 + $c5 ) == 15 ); $total += 2 if ( ( $c3 + $c4 ) == 15 ); $total += 2 if ( ( $c3 + $c5 ) == 15 ); $total += 2 if ( ( $c4 + $c5 ) == 15 ); # count the 3 card combinations that add up to 15 $total += 2 if ( $c1 + $c2 + $c3 == 15 ); $total += 2 if ( $c1 + $c2 + $c4 == 15 ); $total += 2 if ( $c1 + $c2 + $c5 == 15 ); $total += 2 if ( $c1 + $c3 + $c4 == 15 ); $total += 2 if ( $c1 + $c3 + $c5 == 15 ); $total += 2 if ( $c1 + $c4 + $c5 == 15 ); $total += 2 if ( $c2 + $c3 + $c4 == 15 ); $total += 2 if ( $c2 + $c3 + $c5 == 15 ); $total += 2 if ( $c2 + $c4 + $c5 == 15 ); $total += 2 if ( $c3 + $c4 + $c5 == 15 ); # count the 4 card combinations that add up to 15 $total += 2 if ( $c1 + $c2 + $c3 + $c4 == 15 ); $total += 2 if ( $c1 + $c2 + $c3 + $c5 == 15 ); $total += 2 if ( $c1 + $c2 + $c4 + $c5 == 15 ); $total += 2 if ( $c1 + $c3 + $c4 + $c5 == 15 ); $total += 2 if ( $c2 + $c3 + $c4 + $c5 == 15 ); # See if all the cards add up to 15 $total += 2 if ( $c1 + $c2 + $c3 + $c4 + $c5 == 15 ); return $total; } ## end sub fifteens print "hand = A,2,3,4,5 \tTotal =", fifteens ( "A,2,3,4,5"), "\n"; print "hand = 5,5,5,J,5 \tTotal =", fifteens ( "5,5,5,J,5"), "\n"; print "hand = 6,7,8,9,5 \tTotal =", fifteens ( "6,7,8,9,5"), "\n";
There must be a neater way... or is this the cleanest (which I doubt).

2006-03-31 Retitled by planetscape, as per Monastery guidelines
Original title: 'there has to be a better way'


In reply to calculating cribbage points by dracos

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.