Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Why does ‘keys’ need a named hash?

by Anonymous Monk
on Apr 05, 2017 at 21:26 UTC ( [id://1187190]=note: print w/replies, xml ) Need Help??


in reply to Why does ‘keys’ need a named hash?

I read the answers above and feel the issue could be further clarified... The question is about braces and the ways in which they are used in perl.

To work with hashes, two useful syntaxes should be understood.

  • One is anonymous hash constructor: { @list }
  • The other is hash dereference syntax: %{ $href }
  • You can combine the two to produce a hash from a list, and then, make it list again: %{ { @list } }.
    In the process, duplicate keys are squashed.

With map, the braces have an ambiguous meaning: they could be a hash constructor, or (more commonly), a BLOCK. To avoid confusion, you might want to rewrite the map expression using parentheses this time: map(( $_ => 1 ), @list).

Finally, if you intend to use a hash (a data structure that cannot have duplicate keys) to implement the uniq routine, you must at some step construct one! Either anonymous or named one; but for anon case, you'll have the extra braces.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-25 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found