CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [rushadrena] A file parsing and 2D array/matrix problem.: Edit Log



Laurent_R
Enthusiast / Moderator

Aug 30, 2012, 7:46 AM


Views: 4263
Re: [rushadrena] A file parsing and 2D array/matrix problem.

I made a quick attempt to code your problem yesterday, but did not have time to post it before I had to leave the place where I was then. And I'm back in front of the same computer only now.

Here it is:

Code
   use strict;    
use warnings;
use Data::Dumper;

my ($list_1, $list_2) = ("list.txt", "list2.txt");
my %supermatrix1 =read_file ($list_1);
print "\n==============================\n";
my %supermatrix2 = read_file ($list_2, %supermatrix2);
print "\n==============================\n";
my %concat_supermatrix = concat_matrices ();
print Dumper %concat_supermatrix;

sub read_file {
my $list_in = shift;
my %supermatrix;
open my $DATA, '<', $list_in or die "unable to open $list_in $!\n";
while (my $line = <$DATA>) {
chomp $line;
my $substrate = $1 if $line =~ /substrate.{4} (\d+)\s*$/;
$line = <$DATA>;
my @products;
(undef, @products) = split /\s+/, $line;
$supermatrix{$substrate}{$_} = 1 foreach (@products);
}
print Dumper %supermatrix;
return %supermatrix;
}

sub concat_matrices {
my %supermatrix_cat = %supermatrix1;
foreach my $subs (keys %supermatrix2) {
$supermatrix_cat{$subs}{$_} = 1 for keys %{ $supermatrix2{$subs} };
}
return %supermatrix_cat;
}


As you can see, it is just 35 lines of code. It is quite similar to Chris Charley's version, because it is based on the same sparse data structure.

I ran it with the data of your ooriginal post for list_1 and the following data for list2:

Code
 substrate: 7182     
product: 1875
substrate: 2809
product: 3187
substrate: 3186
product: 2810


As you can see the matrices have a very different size.

This is the data dump of list2:

Code
   ==============================    
$VAR1 = '2809';
$VAR2 = {
'3187' => 1
};
$VAR3 = '3186';
$VAR4 = {
'2810' => 1
};
$VAR5 = '7182';
$VAR6 = {
'1875' => 1
};


And this is the data dump of the concatenated matrix:

Code
   ==============================    
$VAR1 = '3489';
$VAR2 = {
'3490' => 1
};
$VAR3 = '3877';
$VAR4 = {
'3419' => 1
};
$VAR5 = '3182';
$VAR6 = {
'1875' => 1
};
$VAR7 = '3649';
$VAR8 = {
'3419' => 1,
'3648' => 1
};
$VAR9 = '3674';
$VAR10 = {
'3489' => 1,
'3490' => 1
};
$VAR11 = '3490';
$VAR12 = {
'3485' => 1
};
$VAR13 = '3675';
$VAR14 = {
'3674' => 1
};
$VAR15 = '3645';
$VAR16 = {
'3647' => 1
};
$VAR17 = '3488';
$VAR18 = {
'3487' => 1
};
$VAR19 = '2809';
$VAR20 = {
'3187' => 1,
'3182' => 1
};
$VAR21 = '3485';
$VAR22 = {
'3486' => 1
};
$VAR23 = '3186';
$VAR24 = {
'2810' => 1,
'2809' => 1
};
$VAR25 = '3487';
$VAR26 = {
'3877' => 1
};
$VAR27 = '3486';
$VAR28 = {
'3488' => 1
};
$VAR29 = '3659';
$VAR30 = {
'3647' => 1
};
$VAR31 = '7182';
$VAR32 = {
'1875' => 1
};




(This post was edited by Laurent_R on Aug 30, 2012, 7:51 AM)


Edit Log:
Post edited by Laurent_R (Enthusiast) on Aug 30, 2012, 7:51 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives