Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Learning about Arrays and Hashes - printing pieces of them

by Your Mother (Archbishop)
on Jan 28, 2015 at 19:29 UTC ( [id://1114802]=note: print w/replies, xml ) Need Help??


in reply to Learning about Arrays and Hashes - printing pieces of them

You have an error in the LOOKUP hashref. Here’s a simplified version that will output what you expect–

use strict; use warnings; my $rec = { TEXT => "Fred loves Ethel", SEQUENCE => [ 13, 27, 47, 98 ], LOOKUP => { cars => 'Buick', dog => 'hound' } }; print $rec->{TEXT}, "\n", $rec->{SEQUENCE}[0], "\n", $rec->{LOOKUP}{dog}, "\n";

Notes — Find the difference between your LOOKUP and mine. Always use strict and warnings (at least when starting). You can pass a list of things to print so only one is necessary. The newlines generally have to be asked for as well. Welcome to the monastery and good luck with Perl! It’s a terrific language.

(Update: LanX usually welcomes newcomers… Sorry you had to settle for me. :P)

Replies are listed 'Best First'.
Re^2: Learning about Arrays and Hashes - printing pieces of them
by Anonymous Monk on Jan 28, 2015 at 19:56 UTC
    Hi Mom!

    I see my dog forgot to point to my hound.... I was missing the > pointing to hound. Thank you!

    What happens when I try:

    print $rec -> [Buick], "\n";
    Does that just print "Buick" or does it print a 0? I didn't find a specific example of that so I was wondering how I find the value Buick.

    Could I do something like this:

    my $favoriteCar = $rec->{LOOKUP}[0]; print "My favorite car is ", $favoriteCar, "\n";
    Thank you!

    Raye Ann

      You can answer these questions by running the code yourself, under the perl built-in debugger.

      Just run you rprogam like this:

      perl -d <Your-program-file-name>
      Inside the debugger prompt , use the "h" (help) command , using "x" check the values of the variables.

      Note: You need to FIRST use the "s" command to get the $rec variable initialized.

      Here are the results of running your program - it includes answers to your questions:

      DB<1> s main::(junk.pl:10): print $rec->{TEXT},"\n", main::(junk.pl:11): $rec->{SEQUENCE}[0],"\n", main::(junk.pl:12): $rec->{LOOKUP}{"dog"},"\n"; DB<1> x $rec -> [Buick] Not an ARRAY reference at (eval 7)[/usr/lib/perl5/5.10.0/perl5db.pl:63 +8] line 2. DB<2> x $rec -> {LOOKUP} -> {cars} 0 'Buick' DB<3> x $rec -> {LOOKUP} [0] Not an ARRAY reference at (eval 9)[/usr/lib/perl5/5.10.0/perl5db.pl:63 +8] line 2. DB<4> x $rec -> {LOOKUP} {dog} 0 'hound' DB<5> q

              "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1114802]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-24 09:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found