Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^8: Is there any difference between prototype % and @ ?

by AnomalousMonk (Monsignor)
on Feb 23, 2013 at 15:13 UTC ( #1020315=note: print w/ replies, xml ) Need Help??


in reply to Re^7: Is there any difference between prototype % and @ ?
in thread Is there any difference between prototype % and @ ?

BrowserUk: Your implementation of  hmap and  hgrep is a little tricky, but I certainly wouldn't describe it as 'quite obfuscated'! However...

sub hmap (&\%) { my( $code, $href ) = @_; map{ local @_; $code->( @_ = each %$href ) } 1 .. keys %$href; }

In the code above, I don't see the point of local-izing  @_ and then assigning  each to it. Passing the return of  each %$href 'directly' to the coderef seems to work just as well.

>perl -wMstrict -MData::Dump -le "sub hmap (&\%) { my( $code, $href ) = @_; map{ $code->(each %$href) } 1 .. keys %$href; } ;; my %h = 'a'..'z'; ;; print join ' ', hmap { qq{$_[0] => $_[1]} } %h; ;; my %orig = ( cat => 22, dog => 23, category => 66, catalyst => 77, cataclysm => 88, dogma => 89, dogstar => 92, ); my %h2 = hmap { $_[0], $_[1] + $_[1] } %orig; dd \%h2; " w => x e => f a => b m => n s => t y => z u => v c => d k => l q => r +g => h i => j o => p { cat => 44, cataclysm => 176, catalyst => 154, category => 132, dog => 46, dogma => 178, dogstar => 184, }


Comment on Re^8: Is there any difference between prototype % and @ ?
Select or Download Code
Re^9: Is there any difference between prototype % and @ ?
by BrowserUk (Pope) on Feb 23, 2013 at 15:20 UTC

    You're right! It's a left-over from starting with a copy of hgrep where I needed to keep a copy of the pair in order to return it conditionally.

    Thanks for pointing out the simplification.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (14)
As of 2014-07-31 19:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (251 votes), past polls