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

Creating a hash with key generated by a sub?

by smullis (Pilgrim)
on Nov 12, 2004 at 12:53 UTC ( #407342=perlquestion: print w/ replies, xml ) Need Help??
smullis has asked for the wisdom of the Perl Monks concerning the following question:

Hello All...

I'm having a problem generating hash keys using a sub. I'm pretty sure that this is a syntactic error and while I could easily "do it another way" I'm just curious as to why this occurs....

Let's set it all up:

use Digest::MD5 qw(md5_hex); use strict; use warnings; my $secret = "applepie";

Here's the sub function:

sub get_md5 { md5_hex("$secret" . "$_") };

This works great...

my @array = qw(foo bar baz); my %md5_other_hash = (map {&get_md5, $_ } @array);

Not this though:

my %md5_hash = ( &get_md5('foo') => "foo", &get_md5('bar') => "bar", &get_md5('bar') => "baz", );

But Why?


Whereas, here we have no problem...

print &get_md5('foo');

Anyone care to help me understand what's going on here? I intuitively think it should be fine.... which is why I ask...

Many thanks in advance,

Cheers

SM

Comment on Creating a hash with key generated by a sub?
Select or Download Code
Replies are listed 'Best First'.
Re: Creating a hash with key generated by a sub?
by gaal (Parson) on Nov 12, 2004 at 13:57 UTC
    Note that your code here:

    sub get_md5 { md5_hex("$secret" . "$_") };

    uses two globals and would probably be better written to use parameters:

    sub get_md5 { my($my_secret, $data) = @_; md5_hex($my_secret . $data); };
Re: Creating a hash with key generated by a sub?
by dragonchild (Archbishop) on Nov 12, 2004 at 12:58 UTC
    What doesn't work? It looks just fine to me ...

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      AAAAAARRRRRGGGGGHHHH!!!!!!!!!!

      Dragonchild, you are - of course - completely correct

      I am, quite simply, being an a*se.

      FYI - my original code, before I copied it into PM - all neat and tidy - didn't quote the hash value. THAT's where the error was from....

      I suppose I can take solace in the fact my intuition was correct - I've just got to learn to trust it a bit more!!


      Cheers

      SM

      ...smullis puts his tail between his legs and runs off to hide....

Re: Creating a hash with key generated by a sub?
by steves (Curate) on Nov 12, 2004 at 13:11 UTC

    You had a few simple errors. This works:

    use Digest::MD5 qw(md5_hex); use strict; use warnings; my $secret = "applepie"; sub get_md5 { md5_hex("$secret" . "$_[0]") }; my @array = qw(foo bar baz); my %md5_hash_broken = (map {&get_md5($_) => $_} @array); my %md5_hash = ( &get_md5('foo') => "foo", &get_md5('bar') => "bar", &get_md5('fubar') => "baz", ); my ($x, $y); while (($x, $y) = each %md5_hash) { print "$x => $y\n"; }

    The errors (see my changes):

    • I got a warning on the use of $_ in get_md5();
    • Your map was not passing an argument to get_md5();
    • Your hash called get_md5() with the argument bar twice.

      Thanks for this steves...

      One point of interest - I thought that $_ was implicitly passed to map?
      The %blah = (map {&get_md5, $_} @array); worked for me fine...

      Cheers,

      SM

        I should clarify: In other places, get_md5() is passed with one argument, which I assumed you wanted concatenated to $secret to make the key. The way map was originally set up, it was not passing anything to get_md5() -- it was calling it with no arguments and using $_ as the hash value. Unless I'm missing something ...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://407342]
Approved by pelagic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2015-07-08 05:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (94 votes), past polls