467,179 Members | 1,235 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,179 developers. It's quick & easy.

Large Matrix into a hash of a hash

Dear all,

I am new to Perl.

I have a large matrix. The easiest way to describe is it is like a calender where there are days along the top, numbers of weeks down the side, and the numbers of dates in the rest of the table.

Imagine this calendar to be populated with different data but with the same concept, but more like 10,000s across and 10,000s down. How would I parse this huge dataset into a hash. I know how to do the smaller example, as you can define the associative parts in the perl code. My file is too large for that. How do you define the top line and the side column into the hash as the important parts?

Kevin
Jun 23 '08 #1
  • viewed: 1471
Share:
8 Replies
A bit more informations:-

I was reading the guidelines, and want to make it clear that I am not asking for code to be written for me. I am just confused as I am new and all the hash examples define the data within the perl code. I am just stating that my file is too large for that, so how do you define the keys and values, using the first row line and the first column. I am just confused by perl toy examples, as they do not show this step with reading a file in.

Your help will be very appreciated.

Kevin
Jun 23 '08 #2
KevinADC
Expert 2GB
use the open() function to open a file and start piping its contents into your perl script. http://perldoc.perl.org/perlopentut.html
Jun 23 '08 #3
I have this which is for a 2 column file that has been fed into a hash:- as I was looking at examples of hashes.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $index1;
  7. my $index2;
  8. my %hashname;
  9. my $hashname;
  10.  
  11. open (LIST1, "test.txt") || die "File not found\n";
  12.      while (<LIST1>) {
  13.           ($index1, $index2) = split(/\t/, $_);
  14.           $hashname{$index1} = $index2;
  15.  
  16.           print $index1;
  17.      }
  18. close(LIST1);
But I want this type of script for a huge dataset with 10,000s of columns each way. So my index1 is my first column, and index2 is my row at the top, rather than the second column in this case.

Kevin
Jun 23 '08 #4
KevinADC
Expert 2GB
Show a sample of the data, with a few rows and columns and explain what you want to do. I don't understand what you said above: "So my index1 is my first column, and index2 is my row at the top, rather than the second column in this case".
Jun 23 '08 #5
0 A B C D E F G --> thousands more
A 1 0 5 1 2 3 1
B 6 0 2 1 1 1 1
C 1 0 2 1 2 1 1
D 2 0 1 2 1 1 1
E 1 1 0 1 1 1 1
F 0 1 0 1 2 1 9
G 0 1 3 2 1 1 1

thousands more

I have to go through the hash of a hash, so the top line is one hash ref and the column down the side is another. I have to pick each number is over 2, and the letter ref of the row and column are stated rather than the number itself.

Example:
A D is 2 so more than 2 (A on top row, D in column)
C A is 5 so more than 2

need to print out A\tD
C\tA
and so on....

I am getting confused how to read it in properly. All the examples of HOH's are small toy examples (Barney, Fred etc....) that are loaded in the code rather than large datasets in a file.

Thanks,

Kevin
Jun 23 '08 #6
KevinADC
Expert 2GB
0 A B C D E F G --> thousands more
A 1 0 5 1 2 3 1
B 6 0 2 1 1 1 1
C 1 0 2 1 2 1 1
D 2 0 1 2 1 1 1
E 1 1 0 1 1 1 1
F 0 1 0 1 2 1 9
G 0 1 3 2 1 1 1

thousands more

I have to go through the hash of a hash, so the top line is one hash ref and the column down the side is another. I have to pick each number is over 2, and the letter ref of the row and column are stated rather than the number itself.

Example:
A D is 2 so more than 2 (A on top row, D in column)
C A is 5 so more than 2

need to print out A\tD
C\tA
and so on....

I am getting confused how to read it in properly. All the examples of HOH's are small toy examples (Barney, Fred etc....) that are loaded in the code rather than large datasets in a file.

Thanks,

Kevin

Unless there is some requirement that I am missing, I see no need for any complex data set to do this task. Process the input file line by line:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. open (LIST1, "test.txt") || die "File not found\n";
  4. my $header = <LIST>; # read first line of file
  5. chomp $header;
  6. my @colms = split(/\s/,$header);
  7. shift @colms; # remove the 0 in the first position so array starts with "A". 
  8. # print "@colms\n"; 
  9. while (<LIST>) {
  10.    chomp;
  11.    my @row = split(/\s/,$_);
  12.    my $this_row = shift @row;
  13.    for my $i (0..$#row) {
  14.       next if $row[$i] < 2;
  15.       print "$colms[$i]\t$this_row \n";
  16.    }
  17. }
  18. close LIST;
Why do you want to use something like a hash of hashes?
Jun 24 '08 #7
Hi Kevin,

Thank you for the code, it worked with my file with the correct output. Well done.

I just wanted to learn how to use a hash of a hash, I am learning perl and trying to practise different ways of solving a problem. To be honest with you, I have always used arrays in perl, and never a hash of hash before, so wanted to try it but for this example dataset I became confused.

I could understand the examples on the internet but they were always loading data within the script, I understand using easy toy examples, but it didn't help when it came to a huge file.

From looking at perl examples on the internet I could see how to split a row into, for example, 5 fields with this particular line:-

while( <FILE> ) {
( $firstname, $surname, $date, $age, $number ) = split( ':' );

I was confused how to do it for a row of 1000s, but I guess just loading it into an array was the best way.

Thank you for you help, it is very appreciated.

Kevin
Jun 24 '08 #8
KevinADC
Expert 2GB
OK, I see, you were more interested in the mechanics of building the data set read in from a file more than anything. If you look at thi spage:

http://perldoc.perl.org/perldsc.html

You will see headings that start with "Generation of....", they show how to build records from files. They don't show the open(FH, 'file.txt') part but just use while (<>) to show that some intput is coming in from a file. Of course they are rather simple examples but hopefully they help.
Jun 25 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

19 posts views Thread by Brad Tilley | last post: by
3 posts views Thread by Gigi.com | last post: by
16 posts views Thread by raj | last post: by
8 posts views Thread by nestini | last post: by
5 posts views Thread by Michael H | last post: by
3 posts views Thread by mediratta@gmail.com | last post: by
14 posts views Thread by James Stroud | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.