The previous answer about an embedded database was appropriate, however, a HoA structure might also be appropriate if you don't need persistence.
I just built one, so here it is:
Hash has UNIX commands as keys. For each hash key, I save an anonymous array of two cells (recent history, and popularity) as value. I then need to sort that hash based on one or the other.
read a file line into $line, then:
my @arry = split(/==/, $line); #recent==pop==commandname
$myhash{$arry[$#arry]} = [$arry[0], $arry[1]];
loop until done
to sort:
my @recentkeys = sort by_HoA_0_asc keys(%myhash);
my @popkeys = sort by_HoA_1_desc keys(%myhash);
# sort Hash of Arrays by numeric value of first
# array value(history), ascending
sub by_HoA_0_asc
{
if ($myhash{$a}[0] < $myhash{$b}[0])
{
return -1;
}
elsif ($myhash{$a}[0] > $myhash{$b}[0])
{
return 1;
}
return 0;
}
# sort Hash of Arrays by numeric value of second
# array value (popularity), descending. If equal,
# sort by first array value (history), ascending.
sub by_HoA_1_desc
{
if ($myhash{$a}[1] > $myhash{$b}[1])
{
return -1;
}
elsif ($myhash{$a}[1] < $myhash{$b}[1])
{
return 1;
}
if ($myhash{$a}[0] < $myhash{$b}[0])
{
return -1;
}
elsif ($myhash{$a}[0] < $myhash{$b}[0])
{
return 1;
}
return 0;
}
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.