How about a little twist on the shredding? 
Author Message
 How about a little twist on the shredding?

   The latest version of the Jason Kastner's "shredded" script is:

   while(<>) {
           ($one, $two, $val) = split(/\s+/);
           if($val > 0) {
                   $counts{$two}++;
           }
   }

   foreach (sort keys %counts) {
           print "$_ $counts{$_}\n";
   }

   Now suppose I want to print the array sorted by count instead of by keys.

   For example, I want:  cats  99  instead of:  afoo  23
                         afoo  23               blah  11
                         dogs  15               cats  99
                         blah  11               dogs  15

   How would I go about efficiently doing that?
--
    Rob Riepel                             |     168 Pine Hall
    Systems Programmer                     |     Stanford, CA 94305-4122
    Networking & Communications Systems    |     (415) 725-7577



Wed, 03 Jul 1996 06:39:13 GMT  
 How about a little twist on the shredding?

Quote:

>   The latest version of the Jason Kastner's "shredded" script is:
  [...]
>   foreach (sort keys %counts) {
>           print "$_ $counts{$_}\n";
>   }

>   Now suppose I want to print the array sorted by count instead of by keys.
  [...]
>   How would I go about efficiently doing that?

Something like

foreach (sort {$counts{$b} <=> $counts{$a}} keys %counts)
{
  print "$_ $counts{$_}\n";

Quote:
}

ought to do the trick...

Mike

--
The "usual disclaimers" apply.    | Meiko
Mike Stok                         | Reservoir Place

Meiko tel: (617) 890 7676         | Waltham, MA 02154



Thu, 04 Jul 1996 01:37:51 GMT  
 How about a little twist on the shredding?

Quote:

>    Now suppose I want to print the array sorted by count instead of by keys.

>    For example, I want:  cats  99  instead of:  afoo  23
>                          afoo  23               blah  11
>                          dogs  15               cats  99
>                          blah  11               dogs  15

>    How would I go about efficiently doing that?

sub bycount { $count{$a} <=> $count{$b}; }

foreach (sort bycount keys(%count)) {
    print "$_ = $count{$_}\n";

Quote:
}



Sat, 06 Jul 1996 01:47:05 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Because twisted minds think twisted things...

2. Summary: my program has been shredded.

3. Please rip my program to shreds!

4. char to number conversion w/ a twist

5. search with a twist

6. s2p twisted

7. Sourcing with a twist of Parsing ?

8. Random Numbers with a Twist

9. A problematic twist with =~ and regex

10. Counter with an NT twist

11. Alphabetizing with a twist

 

 
Powered by phpBB® Forum Software