Solved

Creating a Matrix in perl...

Posted on 2009-04-05
5
1,795 Views
Last Modified: 2012-08-13
I'm doing a computation and that will result in 3 variable (Document name, document value, document term frequency) The values of the computation is in 3 separate variable, so I was thinking of grouping the values into an array or hash so I can sort the result of the computation by the following rules.

The highest document value and the highest term frequency.

here is a sample data

doc1, 0.002, 1
doc2, 0.012, 2
doc3, 0.032, 1

the search Martrix should be like this

doc2, 0.012, 2
doc3, 0.032, 1
doc1, 0.002, 1

How can I create a matrix in perl? or an array of array?
0
Comment
Question by:Trexgreen
  • 3
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 500 total points
ID: 24072777
You can do it in a hash, then use sort on one of the values by providing a sort routine.


#!/usr/bin/perl
 

my %docs;
 

$docs{doc1} = { value => 0.002, freq => 1 };

$docs{doc2} = { value => 0.012, freq => 2 };

$docs{doc3} = { value => 0.032, freq => 1 };
 

print "Unsorted\n";

foreach my $doc (keys %docs) {

   print "$doc $docs{$doc}->{freq}\n";

}
 

print "Sorted\n";

foreach my $doc (sort {$docs{$b}->{freq} <=> $docs{$a}->{freq}} keys %docs) {

   print "$doc $docs{$doc}->{freq}\n";

}

Open in new window

0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24072783
And to change the sort column, change the expression

   $docs{$b}->{freq} <=> $docs{$a}->{freq}

to use whichever column to sort by. Note $b is before $a because you wanted descending sort.
0
 

Author Comment

by:Trexgreen
ID: 24072836
can I sort two columns at once?

I want to have the highes freq and the value
0
 

Author Comment

by:Trexgreen
ID: 24072852
Just found this... I guess I can use this to sort the result

http://search.cpan.org/~dcantrell/Sort-MultipleFields-1.0/lib/Sort/MultipleFields.pm
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24072882
You can, it depends really on what you want. The <=> and cmp operators compare 2 values and return -1, 0 or 1. But you can write a sort function that makes it clearer.

So basically comparing the 1st column, if they are not equivalent, return the sort order just based on 1st column, but if they are equivalent, then return sort based on 2nd.

There may be an even more elegant solution to this, but this is how I would do it.

Keep in mind this is sorting on the fly. If you want to maintain a sorted datastructure with many items, you probably should sort them as you store them.
#!/usr/bin/perl
 

my %docs;
 

$docs{doc1} = { value => 0.002, freq => 1 };

$docs{doc2} = { value => 0.012, freq => 2 };

$docs{doc3} = { value => 0.032, freq => 1 };

$docs{doc4} = { value => 0.032, freq => 2 };
 

print "Unsorted\n";

foreach my $doc (keys %docs) {

   print "$doc $docs{$doc}->{value} $docs{$doc}->{freq}\n";

}
 

print "Sorted\n";

foreach my $doc (sort sort2desc keys %docs) {

   print "$doc $docs{$doc}->{value} $docs{$doc}->{freq}\n";

}
 

sub sort2desc {

   if($docs{$b}->{freq} > $docs{$a}->{freq}) {

      return 1;

   }

   elsif($docs{$b}->{freq} < $docs{$a}->{freq}) {

      return -1;

   }

   else {

      # 1st field equal, so sort by 2nd field

      return $docs{$b}->{value} <=> $docs{$a}->{value};

   }

}

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now