Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Reference Example

by Anonymous Monk
on May 20, 2002 at 15:37 UTC ( #167849=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm ignornant when it comes to some of the more basic concepts of programming, so I'm tackling references. I've come across an example that I can't get to work and would love some insight as to why...

#!/usr/bin/perl -w %animals = ('donald'=>'duck', 'mickey'=>'mouse', 'cheshire'=>'cat'); #set reference for hash $fer = \%animals; print "The value of \$fer is $fer\n"; print "The value for the key \"mickey\" is $$fer['mickey']\n";
... Only when I run this snippet, I get "Not an ARRAY reference at foo.cgi line 7"

Am I blind? Why isn't this correct?

Replies are listed 'Best First'.
Re: Reference Example
by VSarkiss (Monsignor) on May 20, 2002 at 15:44 UTC

    Well, it's not an array reference, as your comment indicates:#set reference for hash
    Try this: print "The value for the key \"mickey\" is $$fer{'mickey'}\n";
    That is, use curly braces {}, not square ones [].

    A slightly cleaner way to write that would be: print "The value for the key \"mickey\" is $fer->{mickey}\n";

    Just as a general comment: if you're trying to get your hands around the basics, references may be a little too much too soon. But I can't really say that applies to you specifically since I don't know. Good luck, HTH.

Re: Reference Example
by Ovid (Cardinal) on May 20, 2002 at 15:50 UTC

    For a good understanding of references, type "perldoc perlreftut" at the command line. It's a great start for learning references.


    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Reference Example
by rinceWind (Monsignor) on May 20, 2002 at 15:48 UTC
    Only when I run this snippet, I get "Not an ARRAY reference at foo.cgi line 7"
    $fer is a HASH reference not an ARRAY reference. You want curly brackets '{' not square brackets.

    Some more advice: learn how to use Data::Dumper. This will enable you to see inside references and deep structures. The first line you print out will not be very meaningful. Data::Dumper's Dumper function will be far more useful.

      ...this was the most concise statement of the answer I saw here.

      The problem was the form of referenced used in the example. (use square brackets for array elements, curly braces for hash elements.)

      I would also second the suggestion made above that references are something to defer until you are certain that you have a good grasp of the basic data structures. But if you feel that you are ready, it's a very powerful mechanism. (just like pointers are in C programming.)


Re: Reference Example
by CukiMnstr (Deacon) on May 20, 2002 at 17:11 UTC
    The other replies already have covered where the error is in your code, as well as some style advice (arrow operator and such), so I won't repeat any of that. But I think you might want to check References quick reference by tye, it will help you understand the relationship between $$foo{$bar}, $foo->{$bar}, ${$foo}{$bar}... (I know it helped me ;)

    hope this helps,

Re: Reference Example
by Beatnik (Parson) on May 20, 2002 at 15:47 UTC
    You have to It's better if you dereference the ref outside the quotes :) and ofcourse do it properly
    print "The value of \$fer is ",%{$fer},"\n"; print "The value for the key \"mickey\" is ",${$fer}{'mickey'},"\n";
    There's ofcourse always Perl's Data Structures Cookbook.

    ... Quidquid perl dictum sit, altum viditur.

      Nitpicky time: one of the reasons for Perl's line-noise rep is lines like:

      print "The value for the key \"mickey\" is ",${$fer}{'mickey'},"\n";
      Using qq instead of the double-quotes and the arrow as the dereferencing operator, you'd get (IMO) a much cleaner:
      print qq(The value for the key "mickey" is $fer->{mickey}\n);

      M-x auto-bs-mode

        Actually, the escaped double quotes were there in the root node. Since the question was about references, I didn't bother to edit THAT part :)

        ... Quidquid perl dictum sit, altum viditur.
Re: Reference Example
by DamnDirtyApe (Curate) on May 21, 2002 at 02:50 UTC
    For the purposes of your example, the first two steps can be condensed into one. Rather than create a hash and then create a reference to it:
    %animals = ('donald'=>'duck', 'mickey'=>'mouse', 'cheshire'=>'cat'); $fer = \%animals;
    Create a reference to an anonymous hash like so:
    $fer = {'donald'=>'duck', 'mickey'=>'mouse', 'cheshire'=>'cat'};
    This will accomplish the same thing.
    D a m n D i r t y A p e
    Home Node | Email
Re: Reference Example
by Anonymous Monk on May 21, 2002 at 16:49 UTC
    This should be $$fer{'mickey'}, not $$fer['mickey']. You want curlies instead of angle brackets, otherwise perl assumes you are dereferencing an array.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://167849]
Approved by VSarkiss
Front-paged by gmax
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2018-07-22 18:46 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (455 votes). Check out past polls.