Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The coming result is not what I expected.

Status
Not open for further replies.

CalmIce

Technical User
Joined
Aug 1, 2006
Messages
1
Location
CN
This is the script:

#!/usr/bin/perl -w

my %last_name = qw [fred flintstone Wilma Flintstone Barney Rubble betty rubble Bamm-Bamm Rubble PEBBLES FLINTSONE];

my @keys=sort
{
"\L$last_name{$a}" cmp "\L$last_name{$b}" or
"\L$a" cmp "\L$b"
} keys %last_name;


foreach my $name (@keys)
{
printf "%-10s%-10s\n",$name,$last_name{$name};
}





The expected result should be:

fred flintstone
PEBBLES FLINTSONE

Wilma Flintstone
Bamm-Bamm Rubble
Barney Rubble
betty rubble

But the output of the script came out like below when I run it on FC4 ,PEBBLES showed first:

PEBBLES FLINTSONE
fred flintstone

Wilma Flintstone
Bamm-Bamm Rubble
Barney Rubble
betty rubble

Could any body help me ? Thanks.
 
I think a typo is causing your problem.

Code:
my %last_name = qw [fred flint[b][blue]stone[/blue][/b] PEBBLES FLINT[b][red]SONE[/red][/b]];

O comes before T, so the sort is working. (-:
 
I was baffled by this, too. May I suggest a more conventional form for defining your array. I think the typo would have been clearer like this ...

Code:
my %last_name = (
    "fred"      => "flintstone",
    "Wilma"     => "Flintstone",
    "PEBBLES"   => "FLINTSONE",
    "Barney"    => "Rubble",
    "betty"     => "rubble",
    "Bamm-Bamm" => "Rubble",
);

my @keys=sort
{
    my $cmp1 = "\L$last_name{$a}" cmp "\L$last_name{$b}";
    my $cmp2 = "\L$a"             cmp "\L$b";
    return ($cmp1 or $cmp2);
} keys %last_name;

foreach my $name (@keys)
{
    printf "%-10s%-10s\n",$name,$last_name{$name};
}

--
-- Ghodmode

Give a man a fish and he'll come back to buy more... Teach a man to fish and you're out of business.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top