simple question
Paul Mooney
pjm at sanger.ac.uk
Mon Nov 13 13:16:51 PST 2006
Hi,
I used the scalar() function on a hash which is not exactly sane but I
just wanted to know roughly what was in it. I expected it to treat the
hash like a list but it did something quite different. Google has not
helped me, neither has my "Programming Perl" (2nd edition).
Can anyone tell me why the scalar value comes out as "5/8" in the code
below? If 5 is the number of keys, is 8 some sort of storage value?
Paul.
#!/usr/local/bin/perl -w
use strict;
my %hash = ('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
print "BEFORE : hash scalar = ", scalar(%hash), "\n";
undef $hash{'a'};
print "AFTER : hash scalar = ", scalar(%hash), "\n";
$hash{'f'} = 6;
print "AGAIN : hash scalar = ", scalar(%hash), "\n";
$hash{'g'} = 7;
print "AGAIN : hash scalar = ", scalar(%hash), "\n";
$hash{'h'} = 8;
print "AGAIN : hash scalar = ", scalar(%hash), "\n";
$hash{'i'} = 9;
print "AGAIN : hash scalar = ", scalar(%hash), "\n";
---Will give---
BEFORE : hash scalar = 5/8
AFTER : hash scalar = 5/8
AGAIN : hash scalar = 6/8
AGAIN : hash scalar = 7/8
AGAIN : hash scalar = 8/16
AGAIN : hash scalar = 9/16
More information about the MiltonKeynes-pm
mailing list