Problem by pushing a String and a Hash on an array!


P

pod69

Hello,

I try to push on an array(global) a string and a hash ->
push(@array,{name => $n_name, hash => \%myhash});

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};
foreach $key ( keys %hash){
print $key." = ".$hash{$key}."\n"
}
}
}

the name gets printed out but not the hash?

thx
 
Ad

Advertisements

G

Gunnar Hjalmarsson

I try to push on an array(global) a string and a hash ->
push(@array,{name => $n_name, hash => \%myhash});

Do you realize that @array only contains one element, i.e. a hash reference?
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};
foreach $key ( keys %hash){
print $key." = ".$hash{$key}."\n"
}
}
}

the name gets printed out but not the hash?

If that's all you have to tell us, you run the code without strictures
and warnings enabled. Please follow the posting guidelines
http://www.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

by adding

use strict;
use warnings;

to the top of your script, and let Perl tell you what's wrong with your
code.
 
P

Paul Lalli

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
 
Ad

Advertisements

M

Mumia W. (on aioe)

Hello,

I try to push on an array(global) a string and a hash ->
push(@array,{name => $n_name, hash => \%myhash});

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}" contains a hash *reference*--not a complete hash. You
must de-reference the hash reference before assigning it to a new hash:

my %hash = %{ $array[$i]{hash} };
foreach $key ( keys %hash){
print $key." = ".$hash{$key}."\n"
}
}
}

the name gets printed out but not the hash?

thx

I hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top