sort hash
am 23.06.2011 11:36:37 von Irfan Sayed
--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--
Re: sort hash
am 23.06.2011 12:07:20 von Rob Dixon
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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: sort hash
am 23.06.2011 12:15:55 von rvtol+usenet
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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: sort hash
am 23.06.2011 12:56:25 von Irfan Sayed
--0-1735254055-1308826585=:19355
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
thanks rob. it worked
________________________________=0AFrom: R=
ob Dixon =0ATo: beginners@perl.org=0ACc: Irfan Sayed
fan_sayed2002@yahoo.com>=0ASent: Thursday, June 23, 2011 3:37 PM=0ASubject:=
Re: sort hash
On 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
It looks like you want to sort the hash elements =
by value?
For string data you must use the string comparator 'cmp' ins=
tead of the=0Anumeric comparator. Take a look at the program below.
HT=
H,
Rob
=0Ause strict;=0Ause warnings;
my %hash_fin =3D (=0A=
=A0 12=A0 =3D> '20110622-035007-134',
131 =3D> '20110622-002139-131',=
107 =3D> '20110621-215838-127',
13=A0 =3D> '20110622-035007-13=
4',
129 =3D> '20110621-235900-129',=0A);
foreach my $key (sort {=
$hash_fin{$b} cmp $hash_fin{$a} } keys %hash_fin) {
printf "%-3d =3D=
> '%s'\n", $key, $hash_fin{$key};=0A}
**OUTPUT**
13=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--