Code Comments
Programming Forum and web based access to our favorite programming groups.
Is there anything wrong with out of the blue making a statement like this:
$p{"Bryan"}{"age"} = 31;
I thought perl required me to use the anonymous hash composer:
$p{"Bryan"} = { "age" => 31 };
... but the first example seems to work. So I was just wondering if that's
okay, or if it's a no-no.
(I'm learning PHP now, and there seem to be a lot of no-nos in PHP that
aren't intuitive.)
- B
Post Follow-up to this messageBryan R Harris wrote:
> Is there anything wrong with out of the blue making a statement like
> this:
>
> $p{"Bryan"}{"age"} = 31;
That's fine, as long as $p{Bryan} doesn't exist or is already a hashref.
See: http://c2.com/cgi/wiki?AutoVivification.
For safety, be sure to "use strict" so you don't end up treating $p{Bryan}
as a soft reference:
$p{Bryan} = 'foo';
$p{Bryan}{age} = '31'; # this is like $foo{age} = 31
Also, the quotes are not strictly required.
>
> I thought perl required me to use the anonymous hash composer:
>
> $p{"Bryan"} = { "age" => 31 };
That's fine too, but not exactly equivalent.
Post Follow-up to this message>>>>> "Bob" == Bob Showalter <Bob_Showalter@taylorwhite.com> writes:
Bob> That's fine, as long as $p{Bryan} doesn't exist or is already a hashref
.
Bob> See: http://c2.com/cgi/wiki?AutoVivification.
And <http://www.stonehenge.com/merlyn/UnixReview/col44.html>.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training
!
Post Follow-up to this messageExcellent! Perl is so! Thanks, Randal. - B > > Bob> That's fine, as long as $p{Bryan} doesn't exist or is already a hashr ef. > Bob> See: http://c2.com/cgi/wiki?AutoVivification. > > And <http://www.stonehenge.com/merlyn/UnixReview/col44.html>.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.