Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Is Perl scary or is it me?

by Golo (Friar)
on Nov 05, 2004 at 23:05 UTC ( [id://405659]=perlmeditation: print w/replies, xml ) Need Help??

Just had an enlightenment:
my %hash = map { join(':', $_->{Computer}, $_->{Bulletin}, ( $_->{Product} || "" ), $_->{Status} ) => $_ } @{GetQueryTable('all_records_for_report', 138)};
or
my %hash; for ( @{GetQueryTable('all_records_for_report', 137)} ) { $hash{"$_->{Computer}:$_->{Bulletin}:" . ($_->{Product}||"") . ":$_->{Status}"} = $_; }

and why did the other way even come to my mind? ;-)

Replies are listed 'Best First'.
Re: Is Perl scary or is it me?
by FoxtrotUniform (Prior) on Nov 05, 2004 at 23:30 UTC
    my %hash = map { join(':', $_->{Computer}, $_->{Bulletin}, ( $_->{Product} || "" ), $_->{Status} ) => $_ } @{GetQueryTable('all_records_for_report', 138)};
    or
    my %hash; for ( @{GetQueryTable('all_records_for_report', 137)} ) { $hash{"$_->{Computer}:$_->{Bulletin}:" . ($_->{Product}||"") . ":$_->{Status}"} = $_; }

    Well, the map-based approach puts the focus on the fact that you're populating %hash. Where you're getting the data from is secondary. The for-based approach puts the focus on the fact that you're doing something with each element in all_records_for_report. What you're doing with it is secondary.

    --
    Yours in pedantry,
    F o x t r o t U n i f o r m

    "Anything you put in comments is not tested and easily goes out of date." -- tye

      mmmh...

      Good point - I see, I won't get around some comments* :-(

      update:
      *comments to where the data comes from, because I think that part is secondary. Now I also know the why :-)

      update2:
      sub make_lookup { my %hash = map { my $hash = $_; join(':', map { $hash->{$_} || "" } sort(keys(%{$_})) ) => $_ } @_; return \%hash; } my $lookup = make_lookup(@{GetQueryTable('all_records_for_report', 138 +)});
Re: Is Perl scary or is it me?
by demerphq (Chancellor) on Nov 06, 2004 at 17:18 UTC

    Theres another distinction besides the ones already mentioned. The first one screams "I'm setting up %hash now for the first time!" the second one depending on where the my %hash; part is located may also be a merge operation. In other words one to me looks like an initialization and the other a merge or aggregation. For instance we may have multiple reports being returned and want to merge them together and for this purpose the first is unsuitable.

    OTOH, if it is an initialization then I personally prefer the first style.

    :-)

    ---
    demerphq

Re: Is Perl scary or is it me?
by VSarkiss (Monsignor) on Nov 06, 2004 at 14:53 UTC

    It's just you. ;-) Relax, you'll get over it.

    But to return to the point brother FU brings up above, it's a question of what you want to emphasize. I think you're talking about how %hash gets populated (a terrible name for the variable, by the way; try to think of a better one), so I might even offer a hybrid:

    my %hash; for ( @{GetQueryTable('all_records_for_report', 137)} ) { $hash{ join(':', $_->{Computer}, $_->{Bulletin}, ( $_->{Product} || "" ), $_->{Status} ) } = $_; }
    If you can think of a more descriptive name for $_, it might clarify your intention quite a bit.

    As Gerry Sussman explained,

    "The important thing about a program is that it's something you can show to people, and they can read it and they can learn something from it ..."
    It's only incidental that computers execute it.

Re: Is Perl scary or is it me?
by !1 (Hermit) on Nov 06, 2004 at 15:51 UTC

    And now for something completely disgusting.

    my %hash; { local $; = ":"; $hash{ @{$_}{qw(Computer Bulletin)}, $_->{Product}||"", $_->{Status} } = $_ for @{GetQueryTable('all_records_for_ +report', 138)}; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://405659]
Approved by FoxtrotUniform
Front-paged by demerphq
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-19 07:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found