in reply to How to (get started on) sort AoA or AoH by frequency
Okay, So without using hashes, here is a sample showing a crude way of getting the sort I need (annotated for clarity):
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @results = (["chpt10_2", "sent. 2", "alice", "nsubj", "animals", "p +rotect"], ["chpt12_1", "sent. 54", "bob", "nsubj", "cells", "prot +ect"], ["chpt25_4", "sent. 47", "carol", "nsubj", "plants", "p +rotect"], ["chpt34_1", "sent. 1", "dave", "nsubj", "cells", "prot +ect"], ["chpt35_1", "sent. 2", "eli", "nsubj", "cells", "prote +ct"], ["chpt38_1", "sent. 1", "fred", "nsubj", "animals", "pr +otect"], ["chpt54_1", "sent. 1", "greg", "nsubj", "uticle", "pro +tect"] ); my @sort_results = sort {lc $a->[4] cmp lc $b->[4]} @results; ##By alp +habet of arg1 my $last_word; my $current_word; my $word_count; $sort_results[-1][6] = 1; ##This weird step is b/c last element didn't + get 7th column appended for my $j (0 .. $#sort_results) { ##[ROW][COLUMN] $current_word = $sort_results[$j][4]; ## current word is arg1 of w +hichever matchset is being looked at (alphabetical) if (lc $last_word eq lc $current_word) { $word_count++; ##If seen before, increment freq. count } else { ##new word if ($j != 0) ##unless it's the first row { for (my $k = 1; $k <= $word_count; $k++) { ##make a new column with freq. Each of the previous see +n word will have to have the same freq. number so iterate back and ma +ke them all the same word count $sort_results[($j-$k)][6] = $word_count; } } ##Now set up for next iteration $last_word = $current_word; $word_count = 1; } } @sort_results = sort {$b->[6] <=> $a->[6]} @sort_results; ##Sort the r +esults by the new 7th freq. column for my $i (0 .. $#sort_results) { print "$sort_results[$i][0], $sort_results[$i][1]: "; ##chptnum, + sent num print "$sort_results[$i][2]\n\n"; ##sentence print "gramatical relation: $sort_results[$i][3]; argument: $sor +t_results[$i][4]; freq: $sort_results[$i][6]\n\n\n"; ##dependency a +rgs }
I would appreciate either a new, better way to do this (I think hashes are the way to get it done), or just an improvement on this crude code. Thanks again for all your help!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to (get started on) sort AoA or AoH by frequency
by Marshall (Canon) on Jun 13, 2011 at 20:32 UTC | |
by jonc (Beadle) on Jun 14, 2011 at 00:28 UTC |