Simon Tomlinson wrote:
> Hi
>
> I want to put a hash into each element of an array. I do it like the
> following bit of code. When I iterate round my hash before putting it in
> the array of the hash values/keys are there. However, when I iterate
> through the hash after putting it in the array all i get is '4/8' as the
> ouput.
>
> I am sure this is very simple and I'm doing something stupid. Does anyone
> have any ideas?
>
> Thanks in advance,
> Simon.
>
> my @array
> my $count = 0;
> my $maxCount = 4;
>
> while $count < $maxCount
> {
> my %myHash = &getHash;
>
> foreach $key (keys %myHash )
> {
> print "THIS WORKS: $key $myHash{$key}\n";
> }
>
> # Now assign the hash to my array
> $array[$count] = %myHash;
>
the '4/8' thingy you see if coming from the above line. the reason:
$array[$count] is scalar context
%myHash is list context
you are assigning %myHash into a scalar context. what Perl gives you is the
ratio of how efficient the hash is used. in your case, it means (in ratio)
4 out of 8 buckets in your %myHash is used. in other word, your $myHash is
50% full. you might have remember that if you assign an array to a scalar
like:
my @array = (1,3,5,7);
my $scalar = @array;
now $scalar contains the number of element in @array. if you assign hash to
a scalar like:
my %hash = (a=>1, b=>2, c=>3);
my $scalar = %hash
Perl won't convert %hash into array and then assign the number of element to
$scalar. what Perl will do is give you the ratio of how efficient your hash
is used. if i run the above, it prints '3/8' which tells me my hash is less
than 50% full.
david
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]