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 hashTry 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.
| [reply] [d/l] [select] |
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.
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
| [reply] |
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.
| [reply] |
|
...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.)
---v
| [reply] |
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, | [reply] |
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.
Greetz
Beatnik
... Quidquid perl dictum sit, altum viditur. | [reply] [d/l] |
|
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);
Chris
M-x auto-bs-mode | [reply] [d/l] [select] |
|
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 :)
Greetz
Beatnik
... Quidquid perl dictum sit, altum viditur.
| [reply] |
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
| [reply] [d/l] [select] |
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. | [reply] [d/l] [select] |