on Jun 20, 2019 at 11:39 UTC ( #11101620=perlquestion: print w/replies, xml ) | Need Help?? |
jimyokl has asked for the wisdom of the Perl Monks concerning the following question:
I just started learning Perl for several days.I sow some codes below, what is the type of $data, is it array or a hash? There are so many nests. Thanks.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: what is the variable type of $data = { by Anonymous Monk on Jun 20, 2019 at 12:06 UTC | |
In this piece of code, { There are two syntactically different ways to access a hash by reference: $data->{$key} (the arrow operator, ->) and ${$data}{$key} (wrap the reference in {} - where you could also put the name of the hash if it had a name). Both ways take $data, try to dereference it, access the underlying hash and return the scalar value referenced by key $key. If $data does not contain a hash reference, an exception will be raised (but if $data is undef, an anonymous hash may be created automatically and a reference to it may be stored inside $data - see autovivification). See perlreftut and perldsc for more info. | [reply] [d/l] [select] |
Re^2: what is the variable type of $data = { by you!!! on Jun 20, 2019 at 12:14 UTC | |
Reputation: 1
| [reply] [edit] [/msg] |
Re: what is the variable type of $data = { | |
++ To AnonyMonk, $data is a reference to a hash, that itself contains hashrefs (eg the value at a10 is a hashref). One way to test for that is to use ref, that will return 'HASH' in this case, or Scalar::Util's reftype, that is guarranted to always return 'HASH' even if the reference is blessed (turned into an object). So you can check that ref $data eq 'HASH' | [reply] [/msg] [d/l] |
Re^2: what is the variable type of $data = { by you!!! on Jun 20, 2019 at 12:21 UTC | |
Reputation: 0
| [reply] [edit] [/msg] |
Re: what is the variable type of $data = { | |
You might be confused because hashes and arrays have to incarnations in Perl
Hashes consist of key => value "string" => scalar
To realize nested hashes you need to put a hash reference into the value slot. That's what's happening here. See perldsc for more examples. HTH :) Cheers Rolf °) my wording |
what is the variable type of $data = {by you!!! |