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

I am trying to learn Perl. I have gotten to the section on lists, arrays and hashes. I read through the tutorials, perlman and perldsc. I want to make sure I understand the following snippet from one of your online tutorials. I've changed some of the values in the pairs to make my questions easier to ask.

$rec = { TEXT => "Fred loves Ethel", SEQUENCE => [ 13, 27, 47, 98 ], LOOKUP => { cars => 'Buick', dog = 'hound' }, THATCODE => \&some_function, THISCODE => sub { $_[0] ** $_[1] }, HANDLE => \*STDOUT, }; print $rec->{TEXT}; print $rec->{SEQUENCE}[0]; print $rec->{LOOKUP}{"dog"};
This is what I think the output would look like (I don't have access to a programming environment at the moment; otherwise, I'd just type stuff in and see what happens):

Fred loves Ethel
13
hound

Am I correct about the output of these print statements?

Thank you for your help as I read through these tutorials to learn Perl.

Raye Ann Whitlow

Replies are listed 'Best First'.
Re: Learning about Arrays and Hashes - printing pieces of them
by Your Mother (Archbishop) on Jan 28, 2015 at 19:29 UTC

    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)

      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