sort hash

--0-210028765-1308821797=:29200
Content-Type: text/plain; charset=us-ascii

Hi,

i need to sort hash in descending order.
but the issue is , in hash, i have key as integer value and the value associated with that key is string

so when i do sort it does not really sort the hash on the key level


i used follwoing code

foreach my $key (sort { $hash_fin{$b} <=> $hash_fin{$a} } keys %hash_fin) {
print "$key => $hash_fin{$key}\n";

}

the unsorted hash is :
12 => 20110622-035007-134
131 => 20110622-002139-131
107 => 20110621-215838-127
13 => 20110622-035007-134
129 => 20110621-235900-129
plz suggest
irfan
--0-210028765-1308821797=:29200--
Irfan Sayed [ Do, 23 Juni 2011 11:36 ] [ ID #2061454 ]

Re: sort hash

On 23/06/2011 10:36, Irfan Sayed wrote:
> Hi,
>
> i need to sort hash in descending order.
> but the issue is , in hash, i have key as integer value and the value associated with that key is string
>
> so when i do sort it does not really sort the hash on the key level
>
>
> i used follwoing code
>
> foreach my $key (sort { $hash_fin{$b}<=> $hash_fin{$a} } keys %hash_fin) {
> print "$key => $hash_fin{$key}\n";
>
> }
>
> the unsorted hash is :
> 12 => 20110622-035007-134
> 131 => 20110622-002139-131
> 107 => 20110621-215838-127
> 13 => 20110622-035007-134
> 129 => 20110621-235900-129

It looks like you want to sort the hash elements by value?

For string data you must use the string comparator 'cmp' instead of the
numeric comparator. Take a look at the program below.

HTH,

Rob


use strict;
use warnings;

my %hash_fin = (
12 => '20110622-035007-134',
131 => '20110622-002139-131',
107 => '20110621-215838-127',
13 => '20110622-035007-134',
129 => '20110621-235900-129',
);

foreach my $key (sort { $hash_fin{$b} cmp $hash_fin{$a} } keys %hash_fin) {
printf "%-3d => '%s'\n", $key, $hash_fin{$key};
}

**OUTPUT**

13 => '20110622-035007-134'
12 => '20110622-035007-134'
131 => '20110622-002139-131'
129 => '20110621-235900-129'
107 => '20110621-215838-127'

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Rob Dixon [ Do, 23 Juni 2011 12:07 ] [ ID #2061455 ]

Re: sort hash

On 2011-06-23 11:36, Irfan Sayed wrote:

> [I need to sort a hash, but my keys are integers]

Realize that all hash keys are strings.


>sort { $hash_fin{$b} <=> $hash_fin{$a} } keys %hash_fin

You were almost there:

sort { $b <=> $a } keys %hash_fin

The <=> operator is numeric, so casts its operands to numeric.

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
rvtol+usenet [ Do, 23 Juni 2011 12:15 ] [ ID #2061456 ]

Re: sort hash

--0-1735254055-1308826585=:19355
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

thanks rob. it worked=0A=0A=0A=0A________________________________=0AFrom: R=
ob Dixon <rob.dixon [at] gmx.com>=0ATo: beginners [at] perl.org=0ACc: Irfan Sayed <ir=
fan_sayed2002 [at] yahoo.com>=0ASent: Thursday, June 23, 2011 3:37 PM=0ASubject:=
Re: sort hash=0A=0AOn 23/06/2011 10:36, Irfan Sayed wrote:=0A> Hi,=0A> =0A=
> i need to sort hash in descending order.=0A> but the issue is , in hash, =
i have key as integer value and the value associated with that key is strin=
g=0A> =0A> so when i do sort it does not really sort the hash on the key le=
vel=0A> =0A> =0A> i used follwoing code=0A> =0A> foreach my $key (sort { $h=
ash_fin{$b}<=3D>=A0 $hash_fin{$a} } keys %hash_fin) {=0A> print "$key =3D>=
=A0 $hash_fin{$key}\n";=0A> =0A> }=0A> =0A> the unsorted hash is :=0A> 12 =
=3D>=A0 20110622-035007-134=0A> 131 =3D>=A0 20110622-002139-131=0A> 107 =3D=
>=A0 20110621-215838-127=0A> 13 =3D>=A0 20110622-035007-134=0A> 129 =3D>=A0=
20110621-235900-129=0A=0AIt looks like you want to sort the hash elements =
by value?=0A=0AFor string data you must use the string comparator 'cmp' ins=
tead of the=0Anumeric comparator. Take a look at the program below.=0A=0AHT=
H,=0A=0ARob=0A=0A=0Ause strict;=0Ause warnings;=0A=0Amy %hash_fin =3D (=0A=
=A0 12=A0 =3D> '20110622-035007-134',=0A=A0 131 =3D> '20110622-002139-131',=
=0A=A0 107 =3D> '20110621-215838-127',=0A=A0 13=A0 =3D> '20110622-035007-13=
4',=0A=A0 129 =3D> '20110621-235900-129',=0A);=0A=0Aforeach my $key (sort {=
$hash_fin{$b} cmp $hash_fin{$a} } keys %hash_fin) {=0A=A0 printf "%-3d =3D=
> '%s'\n", $key, $hash_fin{$key};=0A}=0A=0A**OUTPUT**=0A=0A13=A0 =3D> '2011=
0622-035007-134'=0A12=A0 =3D> '20110622-035007-134'=0A131 =3D> '20110622-00=
2139-131'=0A129 =3D> '20110621-235900-129'=0A107 =3D> '20110621-215838-127'
--0-1735254055-1308826585=:19355--
Irfan Sayed [ Do, 23 Juni 2011 12:56 ] [ ID #2061458 ]
Perl » gmane.comp.lang.perl.beginners » sort hash

Vorheriges Thema: Links to perldoc in pdf
Nächstes Thema: Lock file question