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

what is the variable type of $data = {

by jimyokl (Novice)
on Jun 20, 2019 at 11:39 UTC ( [id://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.
my $data = { 'a10' => { b => 1, a => 2, }, 'bbb' => { x => 3, }, 'a2' => { z => 4, } };

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, { starts a new, anonymous hash (that doesn't have a variable name of its own) and returns a reference to it. The reference is stored in the scalar variable named $data. The hash contains three keys a10, bbb, a2 and the values they have (still scalars) are also hash references.

    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.

      Thank you quite a lot for your answer.
Re: what is the variable type of $data = {
by LanX (Saint) on Jun 20, 2019 at 12:20 UTC
    It's a hash of hashes.(HoH)

    You might be confused because hashes and arrays have two incarnations in Perl

    • a reference which can be stored in a $scalar
    • a "list form" ° which can be stored in variables with appropriate sigils like %hash or @array. (They are effectively containers for references but operating on lists)

    Hashes consist of key => value pairs of scalars ( actually "string" => scalar pairs to be precise)

    To realize nested hashes you need to put a hash reference into the value slot.

    That's what's happening here.

    See perldsc and perlref for more examples.

    HTH :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

    °) my wording

      Thank you, LanX.
Re: what is the variable type of $data = {
by Eily (Monsignor) on Jun 20, 2019 at 12:18 UTC

    ++ 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'

      Thanks for both your quick and detailed answers.
Re: what is the variable type of $data = {
by AnomalousMonk (Archbishop) on Jun 20, 2019 at 16:37 UTC

    There is a third string format in which you may see a reference (any reference!) presented: the string interpolation of a "raw" (i.e., un-dereferenced) reference variable. (In this example,  $blessed_ref has been bless-ed by default into the  main package.)

    c:\@Work\Perl\monks>perl -wMstrict -le "use Scalar::Util qw(reftype) ;; my $blessed_ref = bless { 'foo' => 'bar' }; my $unblessed_ref = { 'bim' => 'bam' }; ;; print 'ref: ', ref $blessed_ref; print 'Scalar::Util::reftype: ', reftype $blessed_ref; ;; print qq{blessed stringization: $blessed_ref}; print qq{unblessed stringization: $unblessed_ref}; " ref: main Scalar::Util::reftype: HASH blessed stringization: main=HASH(0x1825f6c) unblessed stringization: HASH(0x1826098)
    (In this example, I have used the  qq{text} form of the  "text" operator because Windoze uses  " (double-quote) as its command-line delimiter. See Quote and Quote-like Operators and Quote-Like Operators in perlop.)


    Give a man a fish:  <%-{-{-{-<

Re: what is the variable type of $data = {
by ikegami (Patriarch) on Jun 21, 2019 at 21:00 UTC

    $data is a scalar. Its value is a reference to a hash. ({ } returns a reference to a newly-constructed hash.) Similarly, the values of the referenced hash are also reference to hashes. We call this a "Hash of Hash" or "HoH". While it's technically a reference to a hash of references to hashes, the presences of references is implied because scalars (e.g. $data, $data->{a10}) can't contain hashes directly.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11101620]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-03-28 23:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found