[32825] in Perl-Users-Digest
Perl-Users Digest, Issue: 4090 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Dec 5 05:17:40 2013
Date: Thu, 5 Dec 2013 02:17:04 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 5 Dec 2013 Volume: 11 Number: 4090
Today's topics:
Re: weird hash sort question <rweikusat@mobileactivedefense.com>
Re: weird hash sort question <gravitalsun@hotmail.foo>
Re: weird hash sort question <rweikusat@mobileactivedefense.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 04 Dec 2013 10:38:27 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: weird hash sort question
Message-Id: <8738m8rjdo.fsf@sable.mobileactivedefense.com>
ccc31807 <cartercc@gmail.com> writes:
[...]
> SOLVED!!!!
>
> And yeah, I'm kind of excited. I spent most of the afternoon on this instead of working, and got frustrated. My test program and output follows. Comments and criticisms humbly accepted.
> #-------------------sorthash.plx-------------------
> #! perl
> use strict;
> use warnings;
> use List::MoreUtils qw ( indexes );
> use v5.10;
>
> say qq(sorting hash values.........);
> my @campi = qw( T01 M01 D01 ETROY GC T02);
> foreach (@campi) { print " $_ "; }
> print "\n";
>
> my %h = (
> M01 => 44,
> T01 => 456,
> ETROY => 342,
> GC => 498,
> T02 => 9,
> D01 => 108,
> );
>
> foreach my $k (sort keys %h) { say "$k => $h{$k}"; }
> print "-----------------------------\n";
> foreach my $k (sort { $h{$a} <=> $h{$b} } keys %h) { say "$k => $h{$k}"; }
> print "-----------------------------\n";
> foreach my $k (sort {
> my $x = indexes {$_ eq $a} @campi;
> my $y = indexes {$_ eq $b} @campi;
> #print "a $a b $b x $x y $y\n";
> $x > $y ? 1 : -1 ;
> } keys %h) { say "$k => $h{$k}"; }
This is pretty insane coding because it does two linear searches of the
@campi array for each comparison (To a degree. For a small @campi, this
might be faster than doing two hash lookups per comparison or at least,
in can be implemented such that it is. But it's nevertheless quadratic).
It is possible to build a suitable map/ hash with map like this:
my %campi = map { $campi[$_], $_ } 0 .. $#campi;
and then use
foreach my $k (sort { $campi{$a} <=> $campi{$b} } keys %h) { say "$k => $h{$k}"; }
Or just use
for (@campi) { say "$_ => $h{$_}"; }
NB: Both approaches were suggested already.
.
------------------------------
Date: Wed, 04 Dec 2013 16:16:43 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: weird hash sort question
Message-Id: <l7ndgj$n23$1@news.ntua.gr>
Στις 4/12/2013 00:27, ο/η ccc31807 έγραψε:
> What I'd like to do (but doesn't work) is this:
> foreach my $bowl (sort keys %ratings) # this is okay
> foreach my $team (sort (@teams) keys %{$ratings->{$bowl})
> {
> #okay from here on out
> }
> }
>
> Suggestions?
>
> Thanks very, very much, CC.
>
# this is what you want
#!/usr/bin/perl
use strict;
use warnings;
my @teams = ("Florida State","Ohio State","Auburn","Alabama");
my %ratings;
$ratings{BCS}->{"Ohio State"} =1;
$ratings{BCS}->{"Florida State"} =1;
$ratings{BCS}->{"Alabama"} =1;
$ratings{BCS}->{"Auburn"} =1;
foreach my $bowl (sort{ SoA($a,\@teams) <=> SoA($b,\@teams) } keys
%{$ratings{BCS}})
{
print "$bowl\n";
}
sub SoA
{
for (my $i=0; $i < @{$_[1]}; $i++)
{
return $i if $_[1]->[$i] eq $_[0]
}
}
------------------------------
Date: Wed, 04 Dec 2013 17:13:11 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: weird hash sort question
Message-Id: <87pppcpmjc.fsf@sable.mobileactivedefense.com>
George Mpouras <gravitalsun@hotmail.foo> writes:
[...]
> sub SoA
> {
> for (my $i=0; $i < @{$_[1]}; $i++)
> {
> return $i if $_[1]->[$i] eq $_[0]
> }
> }
Perl already knows how to count, consequently, it is not necessary
to tell it how to do that:
for (0 .. $#{$_[1]}) {
return $_ if $_[1]->[$_] eq $_[0];
}
or the more compact
sub ndx {
$_[1]->[$_] eq $_[0] and return $_ for 0 .. $#{$_[1]};
}
This can also be written as
use List::Util qw(first);
sub ndx {
return first { $_[1]->[$_] eq $_[0]} 0 .. $#{$_[1]};
}
Although that's IMO not really preferable.
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 4090
***************************************