Re: Problem by pushing a String and a Hash on an array!
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 30 Dec 2006 14:21:01 -0800
pod69@xxxxxxx wrote:
I try to push on an array(global) a string and a hash ->
push(@array,{name => $n_name, hash => \%myhash});
\%myhash is not a hash. It is a reference to a hash. The distinction
is important.
now i try to read out everything but i have a problems with the array?
I try this to read everything out:
sub println {
for(my $i = 0; $i < scalar(@array) ; $i++ ){
print $array[$i]{name};
my %hash = $array[$i]{hash};
$array[$i]{hash} is a reference to a hash. You are attempting to
create a new hash with this one single scalar value. If you had
enabled warnings, Perl would have told you you were doing something
wrong here. Please ask Perl for help before asking thousands of people
around the world.
You need to dereference the reference. Change this line to:
my %hash = %{$array[$i]{hash}};
or
my $hash_ref = $array[$i]{hash};
foreach $key ( keys %hash){
print $key." = ".$hash{$key}."\n"
If you go with the second option above, change these two lines to:
foreach my $key (keys %{$hash_ref}) {
print $key . " = " . $hash_ref->{$key} . "\n";
}
}
}
the name gets printed out but not the hash?
Please enable warnings, and please read up on references and
multi-dimensional structures.
perldoc perlreftut
perldoc perllol
perldoc perldsc
Paul Lalli
.
- References:
- Prev by Date: Re: Problem by pushing a String and a Hash on an array!
- Next by Date: [Announce] ClieOp2psv
- Previous by thread: Re: Problem by pushing a String and a Hash on an array!
- Next by thread: Re: Problem by pushing a String and a Hash on an array!
- Index(es):
Relevant Pages
|