textual has asked for the wisdom of the Perl Monks concerning the following question:
Now, it works fine if I define Add_key_and_value like that:#!/usr/bin/perl use strict; use warnings; my @array_of_hashes; Add_key_and_value( $array_of_hashes[0], qw/ James Bond / ); print $array_of_hashes[0]{'James'}, "\n"; # Should print 'Bond'.
However, I'd rather define the subroutine like that:sub Add_key_and_value { $_[0]->{ $_[1] } = $_[2]; }
but this yields "use of uninitialized value in print...".sub Add_key_and_value { my( $ref, $key, $value ) = @_; $ref->{ $key } = $value; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Passing a reference to an element in an array of hashes
by roubi (Hermit) on Mar 31, 2009 at 00:35 UTC | |
|
Re: Passing a reference to an element in an array of hashes
by jethro (Monsignor) on Mar 31, 2009 at 00:30 UTC | |
|
Re: Passing a reference to an element in an array of hashes
by graff (Chancellor) on Mar 31, 2009 at 01:22 UTC | |
|
Re: Passing a reference to an element in an array of hashes
by NetWallah (Canon) on Mar 31, 2009 at 05:02 UTC | |
|
Re: Passing a reference to an element in an array of hashes
by textual (Novice) on Mar 31, 2009 at 06:31 UTC |