in reply to Re^17: Why? (each...)
in thread Why? (each...)

One never uses {} or [] to initialise a hash or array. No need to make stuff up about ().

Replies are listed 'Best First'.
Re^19: Why? (each...)
by Argel (Prior) on May 17, 2011 at 00:13 UTC
    The original poster made that exact mistake!!! We're at level 19 and you still haven't figured that out? I guess you just jumped in without reading the entire thread? Impressive trolling!! Well played!! I salute you!! :bow:

    Elda Taluta; Sarks Sark; Ark Arks

      Argel,

      What I think ikegami is trying to point out is this one statement from perldoc:

      List values are denoted by separating individual values by commas (and enclosing the list in parentheses where precedence requires it)

      The only reason the following code doesn't work:

      perl -MData::Dumper -e '%hash = bob => 90, sue => 12; print Dumper(\% +hash);' $VAR1 = { 'bob' => undef };
      ...is because of operator precedence.

      If we look at the operator precedence table we see that assignment "=" comes in at 19 whereas fat comma "=>" and comma "," come in at 20. So in other words, the %hash = bob gets evaluated before the bob => 90, sue => 12 part.

      Up until several minutes ago, when I tried to figure out what ikegami was getting at, I never realized this either. I just blindly memorized and used the (LIST) idiom without understanding why.

      So despite ikegami's seeming inscrutability, I thank him for the new insight.

        I understand. I think Re^11: Why? (each...) comes closest to what I was trying to convey. There's a reason we use syntax like "my %hash = ( x=>1, y=>2, z=>3)" and I was trying to make the point that the reason is passing in a list of key/value pairs (well, it's the most common reason/use/syntax). Or to put it differently, how does mentioning using parens to override precedence help the OP? It's analogous to pasting the source code in. I tried to present a simplified answer that I thought would help the OP in the specific situation that came up in the original question. My question to ikegami about the parens indicating a list was, as I mentioned, in that same context. The purely technical answer is they override precedence. But we do that so we can assign a list of key/value pairs to the hash. And it's that reason that is important. The OP was confused about when to use {} and (), not when to use the comma operator. To reiterate, I went with a less technical but hopefully more useful answer (to the OP). As a side note, in the context being discussed the parens do indicate a list.

        Elda Taluta; Sarks Sark; Ark Arks

      The original poster made that exact mistake!!!

      That's my point. That's the answer you should have given if you were answering "When declaring and initializing hashes (%) and arrays (@), do I use {}, [], or () to assign the values?".

      What you actually said is neither a correct answer for that question nor the one that was actually asked, "I still am fuzzy about the difference between “square brackets,” “braces,” and “parentheses.”"

      We're at level 19 and you still haven't figured that out?

      Actually, I realised he made that mistake and addressed it before you even any of these "19 levels" even existed.

      This thread has been about answering your question, "In the context of this discussion, wouldn't it be that parenthesis indicate a list?".

      And the answer is no. Parens don't indicate a list when initialising a hash. The RHS of the assignment will always be a list a value, and a list literal is indicated by commas.