Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re^2: Foreach in a 2D array

by osunderdog (Deacon)
on Oct 28, 2004 at 13:54 UTC ( [id://403395]=note: print w/replies, xml ) Need Help??

in reply to Re: Foreach in a 2D array
in thread Foreach in a 2D array

This is probably just a minor nit, but thought I might learn something. My understanding is that there is very little difference (from the perl interpreter standpoint) between:

my %hash = ('a', 'b', 'c', 'd');
my %hash = ('a' => 'b', 'c'=> 'd');

Advantages I can think of for the first representation are:

  • As long as you know that a list has an even number of elements, you can use it as a hash.

The advantages I can think of for the second representation are:

  • The => reads better from a maintainability standpoint
  • you don't have to quote the key. For example:
    my %hash = (a=>'b', c=>'d');

Essentially => is syntactic sugar for ,.

Update. Missed a tic.

"Look, Shiny Things!" is not a better business strategy than compatibility and reuse.


Replies are listed 'Best First'.
Re^3: Foreach in a 2D array
by hmerrill (Friar) on Oct 28, 2004 at 18:10 UTC
    You're right. Sorry, I was a little quick to correct the "," to "=>", but I think the comma works fine as you pointed out. I'm so used to using "=>" that at first glance I thought using the comma was illegal. The "as long as you know that the list has an even number of elements..." part is enough for me to use the 2nd version ("=>") since I know the 2nd one works all the time :-) In fact all the perl documentation on hashes shows the "=>" operator.

    As I think you'll agree, in this code:

    my %hash = ('a', 'b', 'c', 'd');
    it is not clear that 'a' is the key referring to value 'b'.

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-06-25 06:15 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.