C
ccc31807
Suggestions for dealing with a warning for a sort function, please.
My query of a database table returns a reference to a hash of all the
records in the database. $content contains the hash reference for the
records. One of the columns is called 'name' which contains values
like 'George Washington', 'John Adams', and 'Thomas Jefferson'. I want
to iterate through the hash, sorting by the last name, e.g., Adams,
Jefferson, and Washington. The first name is immaterial.
The key is 'username' which is the primary key in the table and a
unique value.
This is the code I use to iterate through the hash:
foreach my $key (sort sortf keys %{$content})
{
#print values here, like
$content->{$key}{'name'}
$content->{$key}{'status'}
$content->{$key}{'username'}
$content->{$key}{'role'}
}
Here is the sortf function, which is an inner subroutine in the block
containing the hash iteration:
sub sortf
{
my ($alast, $blast);
($alast = $content->{$a}{'name'}) =~ s/.* (.*)$/$1/; #---line 164---
($blast = $content->{$b}{'name'}) =~ s/.* (.*)$/$1/;
return lc($alast) cmp lc($blast) if $alast ne $blast;
return $a cmp $b;
}
If I place this subroutine outside the block, it can't see $content
and generates a syntax error. If I place it in the block, it runs, but
generates a warning message. This has been running for some time, and
I'm inclined to ignore the warnings, but I would like to know that the
warnings are surplus.
Here is the warning message:
perl -cw CONSOLE.pm
[Fri Oct 2 10:26:09 2009] CONSOLE.pm: Variable "$content" will not
stay shared at CONSOLE.pm line 164.
CONSOLE.pm syntax OK
Suggestions?
TIA, CC.
My query of a database table returns a reference to a hash of all the
records in the database. $content contains the hash reference for the
records. One of the columns is called 'name' which contains values
like 'George Washington', 'John Adams', and 'Thomas Jefferson'. I want
to iterate through the hash, sorting by the last name, e.g., Adams,
Jefferson, and Washington. The first name is immaterial.
The key is 'username' which is the primary key in the table and a
unique value.
This is the code I use to iterate through the hash:
foreach my $key (sort sortf keys %{$content})
{
#print values here, like
$content->{$key}{'name'}
$content->{$key}{'status'}
$content->{$key}{'username'}
$content->{$key}{'role'}
}
Here is the sortf function, which is an inner subroutine in the block
containing the hash iteration:
sub sortf
{
my ($alast, $blast);
($alast = $content->{$a}{'name'}) =~ s/.* (.*)$/$1/; #---line 164---
($blast = $content->{$b}{'name'}) =~ s/.* (.*)$/$1/;
return lc($alast) cmp lc($blast) if $alast ne $blast;
return $a cmp $b;
}
If I place this subroutine outside the block, it can't see $content
and generates a syntax error. If I place it in the block, it runs, but
generates a warning message. This has been running for some time, and
I'm inclined to ignore the warnings, but I would like to know that the
warnings are surplus.
Here is the warning message:
perl -cw CONSOLE.pm
[Fri Oct 2 10:26:09 2009] CONSOLE.pm: Variable "$content" will not
stay shared at CONSOLE.pm line 164.
CONSOLE.pm syntax OK
Suggestions?
TIA, CC.