Well, totally bored here at work. I have a bunch of VBScript code (ugh!) that's sitting here doing nothing until I get more specs. Thought I would use the time to play around with a problem suggested in
another node. Essentially, the author wanted to sort a list by frequency of the items in that list. Having spare time and a huge desire to maintain my limited Perl skills, I wrote the following snippet:
use warnings;
use strict;
use Data::Dumper;
my @vals = qw( 1 2 3 1 3 3 3 43 bob 2 bob 6 2 );
my %freqs;
$freqs{$_}++ foreach @vals;
my @sorted = map { $_->[0] }
sort { $a->[1] <=> $b->[1] }
map { [ $_, $freqs{ $_ } ] } keys %freqs;
print Dumper( \@sorted );
Is there any way of eliminating the temporary variable
%freqs? I hate synthetic code (code that solves a coding problem, not the problem the program is designed for) and it's bugging me. Or is this one of those age-old "FAQ" questions that I should already know the answer to?
Yeah, I know. It's a trivial post, but like I said, I am bored out of my skull here.
Incidentally, I am returning to the US in April (and will be programming in Perl again!!!!) and will be bidding a sad farewell to my newfound friends here in the Netherlands: kudra, ar0n, and h0mee (who was here visiting).
Cheers,
Ovid
Update: Once again, tilly makes me feel stupid :)
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
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.