Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Access to hash ref element

by Anonymous Monk
on May 28, 2013 at 17:31 UTC ( #1035661=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there Monks!
Its a question of doing it better, so I am confused!
How to access or better print the value of "name" from this simple sample code structure:
use strict; use warnings; use Data::Dumper; my $data = [ { 'name' => 'Mary' }, ]; #my $test = $data[0]{1}; #my $test = $ { $data} { name }; #my $test = @{ $data} { name }; my $test = $data->{ name }; print "\n - $test \n"; warn Dumper $test;
Thanks for looking!

Replies are listed 'Best First'.
Re: Access to hash ref element
by choroba (Archbishop) on May 28, 2013 at 17:33 UTC
    The [ at the beginning of the structure means it is an array reference. The following { tells us there is a hash reference inside. Therefore, the following should work:
    my $test = $data->[0]->{'name'};

    which can be shortened to

    my $test = $data->[0]{name};

    as arraows after ] and } are optional and simple alphanumeric keys are quoted automatically.

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      That's right! Thanks for it!
Re: Access to hash ref element
by Laurent_R (Canon) on May 28, 2013 at 17:40 UTC

    Hi, you could do:

     print $data->[0]{name};

    Or, if you prefer:

    print $data->[0]->{name}

    EDIT: oops, I had not seen Choroba's answer.

Re: Access to hash ref element
by TomDLux (Vicar) on May 28, 2013 at 20:01 UTC

    Seems to be a popular question for newcomers to ask how to access some component of a complicated structures.

    The answer is to user the built-in debugger.

    tlegrady@rw-q318:~$ perl -d t.pl Loading DB routines from perl5db.pl version 1.3 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(t.pl:5): my $data = [ DB<1> n main::(t.pl:16): my $test = $data->{ name }; DB<1> x $data 0 ARRAY(0x70b910) 0 HASH(0x70b970) 'name' => 'Mary' DB<2> x $data->[0] 0 HASH(0x70b970) 'name' => 'Mary' DB<3> x $data->[0]{name} 0 'Mary'

    When the debugger starts, it shows that it's about to execute line 5. I do a "next" ( 'n') to complete that, and then it's going to execute line 16. I query the debugger to show the value of $data, and then of its components. Since you're looking right at it, it's easy to tell you want array element '0', hash tag 'name'.

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

      Another "answer" is simply Data::Dumper. It produces a dump that shows both the structure's content, and how to access individual elements. All that needs to be done is to dump the structure, and see how it's built.


      Dave

      I fully agree. The debugger (and especially the x command to unfold data structure) is of tremendous help for all such cases and has helped me out many times. But, for some reason, many people seem to be afraid to use it, although it is in fact a very simple tool to use. Many questions on many developping forums would never be asked if the OP had "dared" to use the debugger to figure out what is going on. But there is nothing "daring" there, and it is increedibly helpful.

      Having said that, the Data::Dumper is also a very useful way of understanding complicated data structure. But for the purpose of debugging, I think the debugger is the right tool.

        the debugger may be great for debugging your own programs, but not particularly friendly for sharing on forums -- Dumper fits that bill better
Re: Access to hash ref element
by AnomalousMonk (Bishop) on May 28, 2013 at 19:12 UTC

    ... and if you want to interpolate it into a string before you print it, such scalar elements of arrays and hashes, however deeply nested or directly or indirectly referenced, interpolate like any other scalar:

    >perl -wMstrict -le "my $data = [ {'name' => 'Mary'} ]; ;; print qq{---$data->[0]{name}---}; " ---Mary---

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2020-10-26 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (249 votes). Check out past polls.

    Notices?