perlboer has asked for the wisdom of the Perl Monks concerning the following question:
my (%hash); my $data = "$BaseDir/perf.log"; open STUFF, $data or die "cannot open for read $!"; while ( <STUFF>) { chomp; #my $X = "html"; #next if ( $_ !~ /^$X/ ); # Only look at lines begin with th +e topic next if ( $_ !~ /^[a-z]/ ); # Only look at lines begin with +the topic my ($topic, $desc, $url, $hits) = split "," ; if ($topic){ ${hash}{$url}{topic} = $topic if ($topic); ${hash}{$url}{desc} = $desc if ($desc); ${hash}{$url}{url} = $url if ($url); ${hash}{$url}{hits} = $hits if ($hits); } } close STUFF; print Dumper %hash , "\n"; print "Sort Numeric using <=> with supersort\n"; foreach my $sorted_url ( reverse sort { $hash{$a}->{'hits'} <=> $hash{ +$b}->{'hits'} } keys %hash){ print $sorted_url, " has got ", ${hash}{$sorted_url}{hits}, " +hits \n";
output: (not sorted by number!!!!!)# -|print_topic|------------------------------------------------------ +--------- # Pre : t_topic # Post: # -------------------------------------------------------------------- +--------- sub print_topic ($) { my $t_topic = shift; open( INFILE, "$conf_file" ) or die "$!"; while (<INFILE>) { chomp; my (%hash); next if 0 == length($_); # Solves "Use of uninitialized va +lue in .......... " next if ( $_ !~ /^$t_topic/ ); # Only look at lines begin +with the t_topic my ($topic, $desc, $url, $hits) = split "," ; if ($topic) { ${hash}{$url}{topic} = $topic if ($topic); ${hash}{$url}{desc} = $desc if ($desc); ${hash}{$url}{url} = $url if ($url); ${hash}{$url}{hits} = $hits if ($hits); } #print "Sort Numeric using <=> with supersort\n"; #print Dumper %hash , "\n"; foreach my $sorted_url ( reverse sort { $hash{$a}->{'hits'} <= +> $hash{$b}->{'hits'} } keys %hash){ print $sorted_url, " has got ", ${hash}{$sorted_url}{hits}, "\ +n"; } } close INFILE; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sorting hash ref
by cool_jr256 (Acolyte) on Jun 10, 2005 at 14:13 UTC | |
by perlboer (Acolyte) on Jun 10, 2005 at 14:30 UTC | |
|
Re: sorting hash ref
by tlm (Prior) on Jun 10, 2005 at 14:22 UTC | |
by salva (Canon) on Jun 10, 2005 at 14:43 UTC | |
by halley (Prior) on Jun 10, 2005 at 14:59 UTC | |
|
Re: sorting hash ref
by Samy_rio (Vicar) on Jun 10, 2005 at 14:57 UTC | |
by salva (Canon) on Jun 10, 2005 at 15:24 UTC | |
by Samy_rio (Vicar) on Jun 11, 2005 at 09:40 UTC |