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



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

.



Relevant Pages

  • Re: Complex Objects in Perl
    ... > Objects in Perl are implemented as a reference to an ... > anonymous hash. ... > array or a hash or it may be another object (reference to ... > installation of perl: ...
    (comp.lang.perl)
  • Re: Accessing hash within an array of hashes
    ... perldoc perlreftut ... devices_array_oH is set up as array of hashes ... each hash. ... Reference found where even-sized list expected ...
    (perl.beginners)
  • Re: Complex Objects in Perl
    ... >> Objects in Perl are implemented as a reference to an anonymous hash. ... >> enumerate over array and hash members as you would enumerate over any ...
    (comp.lang.perl)
  • Re: Complex Objects in Perl
    ... > sub-object is made up of a number of different objects and an array of an ... Objects in Perl are implemented as a reference to an anonymous hash. ...
    (comp.lang.perl)
  • Re: Accessing variables
    ... What you have there is a hash with one key and no value. ... warnings enabled then perl would have warned you about that. ... print Dumper \%fileresults; ... perldoc perldata ...
    (perl.beginners)