Hello Perlmonks,
My question is related to Re^4: fetch url
I'm almost there .............

input:
# topic,description,url,hits,
topic,description,url,10,
topic,description,url2,4,
topic,description,url3,6,
topic,description,url4,3,

snippet:
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:
Sort Numeric using <=> with supersort
url has got 10 hits
url3 has got 6 hits
url2 has got 4 hits
url4 has got 3 hits

Just we want to have.




Question: I use the same technique in a subroutine
input: (same)
# topic,description,url,hits,
topic,description,url,10,
topic,description,url2,4,
topic,description,url3,6,
topic,description,url4,3,
snippet:
# -|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; }
output: (not sorted by number!!!!!)

topic,description,url,10,
topic,description,url2,4,
topic,description,url3,6,
topic,description,url4,3,
Do you have any clue ?

Thank you in advance
Perlboer

In reply to sorting hash ref by perlboer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.