Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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).


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.

In reply to Sort on boredom by Ovid

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2023-03-22 09:04 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.