SPUG:Reference question
Thane Williams
thane at fastmail.fm
Sun Mar 9 16:28:17 CST 2003
Ok, maybe someone can point out the obvious to me here. Here's my code:
#!/usr/bin/perl
my %record;
$record{Wilma}{age} = 32;
$record{Fred}{age} = 38;
$record{Wilma}{sex} = "female";
$record{Fred}{sex} = "male";
foreach my $key (keys %record) {
my $age = $record{$key}{age};
my $sex = $record{$key}{sex};
foreach ("age", "sex") {
print "$_ for $key is ${$_}\n";
}
}
The foreach loop prints this:
age for Fred is
sex for Fred is
age for Wilma is
sex for Wilma is
If $_ equals "age", I'd expect ${$_} to equal $age.. which it apparently
doesn't. (I get the same results even if I put "my $age = 50;").
I tried this test code:
#!/usr/bin/perl
$name = "Fred";
$age = "55";
$sex = "male";
foreach ("age", "sex") {
print "$_ for $name is ${$_}\n";
}
And it works! What's the difference?
Thanks in advance..
More information about the spug-list
mailing list