Do you mean something like this...?
Regular expression with capture
$games{person} = "11::Game name::Game date";
foreach (sort keys %games) {
my ($count, $rest) = $games{$_} =~ /^([^:]+)::(.*)/;
print "<td>$count</td><td>$rest</td>...";
}
Somehow I have a feeling that you are actually looking for a way to sort your hash by each person's score?
use strict;
use warnings;
use Data::Dumper;
my %games;
$games{person1} = "11::Game name::Game date";
$games{person2} = "12::Game name::Game date";
$games{person3} = "9::Game name::Game date";
# sort by score, and record sorted list of hash keys
my @sorted =
map { $_->[0] }
sort { $b->[1] <=> $a->[1] }
map { [ $_, (split /::/,$games{$_})[0] ] }
keys %games;
foreach (@sorted) {
# my ($count, $rest) = $games{$_} =~ /^(\d+)::(.*)/;
# I like thor's split with limit suggestion below,
# certainly looks cleaner.
my ($count, $rest) = split/::/,$games{$_},2;
print "<td>$_</td><td>$count</td><td>$rest</td>...\n";
}
And now combile the Schwartzian transform and the foreach to get the optimized form -
...
print "<td>$_->[1]</td><td>$_->[2]</td>...\n"
for sort { int $b->[1] <=> int $a->[1] }
map { [ $_, split /::/, $games{$_}, 2 ] }
keys %games;
...
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.