ady has asked for the wisdom of the Perl Monks concerning the following question:
Could anyone pls. explain that construct ?$var{ 'a', 1, ... } emulates a multidimensional array
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: $var{'a',1,...}
by davido (Cardinal) on May 22, 2005 at 08:32 UTC | |
Straight from the Camel's mouth:
I highly recommend the Camel book: Programming Perl, 3rd Edition, published by O'Reilly & Associates. And of course there is also the POD: perlvar:
It's a syntax you just don't see that often. Most people who know about its existance are already so comfortable with complex datastructures built by using Perl's references that this syntax is set aside in favor of a more powerful tool. Dave | [reply] [d/l] [select] |
by ysth (Canon) on May 22, 2005 at 19:37 UTC | |
Most people who know about its existance are already so comfortable with complex datastructures built by using Perl's references that this syntax is set aside in favor of a more powerful tool.It can be handy when dealing with large, sparse, data structures, where references would take too much memory. | [reply] |
Re: $var{'a',1,...}
by Roger (Parson) on May 22, 2005 at 08:35 UTC | |
This shows that the above is really...
| [reply] [d/l] [select] |
by benizi (Hermit) on May 24, 2005 at 19:19 UTC | |
Careful when using Data::Dumper to dump strings. produces the counterintuitive:
But: reveals:
("\x{001c}" eq "\034" eq chr(28) eq $; See first reply.) | [reply] [d/l] [select] |
Re: $var{'a',1,...}
by tlm (Prior) on May 22, 2005 at 13:36 UTC | |
In addition to what davido posted, let me point out that "the space between the curlies" is a magical place, and exploring all its mysteries can lead to countless epiphanies. For example, there you will find an apparent violation of the Perl dogma that says that "a list can't exist in scalar context." The context between the curlies is a scalar context; this is why, for example, in the following DB interaction, the second line initializes $hash{ 3 }: Now, in a scalar context, a comma-separated list should evaluate to the last member of the list. Therefore, should amount to initializing $hash{ 'baz' }. Instead, it appears (to those who are not pure of soul) as though the comma-separated list survives long enough in this scalar context for it to be shellacked into a join( $;, LIST ) thingie. This apparent violation of dogma is resolved by declaring that the commas in $hash{ 'foo', 'bar', 'baz' } are not comma operators; they receive instead special syntactic dispensation from the Holy Interpreter (I'm sure there is a bull or two written on this somewhere). But the wonders don't end there. It is not just textual commas that receive a special dispensation, but also =>'s, and even the implicit commas in qw() expressions, which leads to great marvels such as I am sure one can derive hours of edifying meditation from variations of these examples. One last thing: the commas do not get in the way of the auto-quoting that happens inside the curlies. E.g. $hash{ 'foo', 'bar' } and $hash{ foo, bar } refer to the same thing. the lowliest monk | [reply] [d/l] [select] |
by ady (Deacon) on May 23, 2005 at 06:21 UTC | |
One could consider this idiom "The Perl Way" in-a-nutshell, --- expanding the syntax of one fundamental datastruct (hash) to emulate another (MD array), and of course there's several ways to do that... This flexibility originating in a wish to make the language dance to the pipe of the programmer's needs & expectations, that's what makes it an effective an tool to use. And of course, --hours of fun to learn. "Ca, ce n'est pas une Pipe" -- allan | [reply] |
Re: $var{'a',1,...}
by ady (Deacon) on May 22, 2005 at 11:42 UTC | |
That shortcut was a real AHA-experience for me... but very much in the spirit of Perl of course. Rather smart actually! for a quick MD-array, though i do see your pt. Dave vith resp. to using refs. for building up complex datastructs. for larger apps. I agree on that. I did read (ride) the Camel; -- some months back by now, so i didn'n recognize the construct, tho' i must have seen it there. -- Much stuff crammed into that big hump :) I didn't think of using Data::Dumper to clearify exactly what this construct might map to in terms of Perl internal dstructs (tho' i've used it in other contexts). But yes, i do see now, how that could have resolved the issue! Thanks' to both of you, -- yet another idiom in the bag (almost like collecting butterflies... best regards -- allan =========================================================== As the eternal tranquility of Truth reveals itself to us, this very place is the Land of Lotuses -- Hakuin Ekaku Zenji | [reply] |
Re: $var{'a',1,...}
by tphyahoo (Vicar) on May 23, 2005 at 08:28 UTC | |
Which is it? Or am I overlooking something obvious? | [reply] [d/l] |
by thinker (Parson) on May 23, 2005 at 09:03 UTC | |
You are overlooking something obvious :-)
cheers thinker | [reply] [d/l] |