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:
A file parsing and 2D array/matrix problem.

 

First page Previous page 1 2 3 Next page Last page  View All


Laurent_R
Veteran / Moderator

Aug 30, 2012, 7:46 AM

Post #26 of 62 (5931 views)
Re: [rushadrena] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

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)


rushadrena
Novice

Sep 4, 2012, 4:27 AM

Post #27 of 62 (5908 views)
Re: [Laurent_R] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Hi all,
I'm facing a small problem. Yesterday while parsing all the files I tried to run test the matrix generation code first and found out that its not giving complete output. Here's the problem
LIST.txt
substrate: 1 2
product: 3
substrate: 6 9
product: 8 10
substrate: 3
product: 6
substrate: 9
product: 5
substrate: 5
product: 2
substrate: 3
product: 9
substrate: 8
product: 9
substrate: 8
product: 1
substrate: 7
product: 11
substrate: 19
product: 17
substrate: 14
product: 13
substrate: 14
product: 11
substrate: 18
product: 19
substrate: 7 14
product: 15
substrate: 7 16
product: 7 17
substrate: 5
product: 6
substrate: 18 15
product: 7
substrate: 7 8
product: 8 18
substrate: 6
product: 9
substrate: 11
product: 12




SUPERLIST_SUBSTRATE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19


SUPERLIST_PRODUCT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19


********OUTPUT********


Code
    p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 p13 p14 p15 p16 p17 p18 p19 
s1 . . 1 . . . . . . . . . . . . . . . .
s2 . . . . . . . . . . . . . . . . . . .
s3 . . . . . 1 . . 1 . . . . . . . . . .
s4 . . . . . . . . . . . . . . . . . . .
s5 . 1 . . . 1 . . . . . . . . . . . . .
s6 . . . . . . . 1 1 1 . . . . . . . . .
s7 . . . . . . 1 1 . . 1 . . . 1 . 1 1 .
s8 1 . . . . . . . 1 . . . . . . . . . .
s9 . . . . 1 . . . . . . . . . . . . . .
s10 . . . . . . . . . . . . . . . . . . .
s11 . . . . . . . . . . . 1 . . . . . . .
s12 . . . . . . . . . . . . . . . . . . .
s13 . . . . . . . . . . . . . . . . . . .
s14 . . . . . . . . . . 1 . 1 . . . . . .
s15 . . . . . . . . . . . . . . . . . . .
s16 . . . . . . . . . . . . . . . . . . .
s17 . . . . . . . . . . . . . . . . . . .
s18 . . . . . . 1 . . . . . . . . . . . 1
s19 . . . . . . . . . . . . . . . . 1 . .


Consider first two lines of LIST.txt -
substrate: 1 2
product: 3
Then there is a "1" s1-p3, but there isn;t a "1" for s2-p3.
#####REQUIRED OUTPUT--Places marked with "X" need to be "1" #####

Code
    p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 p13 p14 p15 p16 p17 p18 p19 
s1 . . 1 . . . . . . . . . . . . . . . .
s2 . . X . . . . . . . . . . . . . . . .
s3 . . . . . 1 . . 1 . . . . . . . . . .
s4 . . . . . . . . . . . . . . . . . . .
s5 . 1 . . . 1 . . . . . . . . . . . . .
s6 . . . . . . . 1 1 1 . . . . . . . . .
s7 . . . . . . 1 1 . . 1 . . . 1 . 1 1 .
s8 1 . . . . . . X 1 X . . . . . . . X .
s9 . . . . 1 . . . . . . . . . . . . . .
s10 . . . . . . . . . . . . . . . . . . .
s11 . . . . . . . . . . . 1 . . . . . . .
s12 . . . . . . . . . . . . . . . . . . .
s13 . . . . . . . . . . . . . . . . . . .
s14 . . . . . . . . . . 1 . 1 . X . . . .
s15 . . . . . . X . . . . . . . . . . . .
s16 . . . . . . X . . . . . . . . . X . .
s17 . . . . . . . . . . . . . . . . . . .
s18 . . . . . . 1 . . . . . . . . . . . 1
s19 . . . . . . . . . . . . . . . . 1 . .


THE CODE I USED

Code
use Modern::Perl; 
use File::Slurp qw/read_file/;
use Text::Table;
use Data::Dumper;

my ( %supermatrix, @titles, %seen, @rows );

my @list = read_file 'LIST.txt';

for ( my $i = 0 ; $i < $#list + 1 ; $i += 2 ) {
my ($substrateID) = $list[$i] =~ /(\d+)/g;
$supermatrix{$substrateID}{$1} = 1 while $list[ $i + 1 ] =~ /(\d+)/g;
}

for my $product ( read_file 'SUPER_PRO.txt' ) {
my ($productID) = $product =~ /(\d+)/g;
push @titles, $productID unless $seen{$productID}++;

for my $substrate ( read_file 'SUPER_SUB.txt' ) {
my ($substrateID) = $substrate =~ /(\d+)/g;
$supermatrix{$substrateID}{$productID} //= '.';
}
}

my $titles = join ',',
map "{title => 'p$_', align_title => 'center', align => 'center'}",
sort { $a <=> $b } @titles;

for my $y ( sort { $a <=> $b } keys %supermatrix ) { #rows
my ( $rowLable, @row );

for my $x ( sort { $a <=> $b } keys %{ $supermatrix{$y} } ) { #columns
$rowLable = $y unless $rowLable;
push @row, $supermatrix{$y}{$x};
}
push @rows, [ "s$rowLable", @row ];
}

my $tb = Text::Table->new( ' ', eval $titles );
$tb->load(@rows);
say $tb;



Laurent_R
Veteran / Moderator

Sep 4, 2012, 10:43 AM

Post #28 of 62 (5869 views)
Re: [rushadrena] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Hi,

you did not say that there could be several substrates on the same line, you said there could be several products on product lines, but you did not say it for substrates.

The code I provided and the one by Chris therefore does not look for possibe other substrate on the substrate line. And you code, probably derived in part from what Chris and myself have suggested, also doesn't check for other substrates on substrate lines.

You have to change the second and third lines of this part of your code to look for possible other substrates:


Code
for ( my $i = 0 ; $i < $#list + 1 ; $i += 2 ) {  
my ($substrateID) = $list[$i] =~ /(\d+)/g;
$supermatrix{$substrateID}{$1} = 1 while $list[ $i + 1 ] =~ /(\d+)/g;
}



Chris Charley
User

Sep 4, 2012, 11:23 AM

Post #29 of 62 (5863 views)
Re: [Laurent_R] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Yes, Laurent found the problem - you need to check for more than 1 substrate on it's line. In addition, when I ran the code you posted, I found other errors but I haven't been able to find the cause. Here is my code modified from my original post. It checks for more than 1 substrate (on 1 line).

Code
 #!/usr/bin/perl  
use strict;
use warnings;
use List::Util qw/ max /;

my @matrix;
my $path = '.'; # (current directory - '.') or path to data files

my @file = qw/list.txt one.txt another.txt/;

for my $file ( @file ) {
my %data;
my @substrate;

open my $fh, "<", "$path/$file"
or die "Unable to open $file for reading. $!";

while (<$fh>) {
if (/^substrate/) {
@substrate = /\d+/g;
}
elsif (/^product/) {
while (/(\d+)/g) {
for my $sub (@substrate) {
$data{$sub}{$1} = 1 ;
}
}
}
else {
die "Unknown format $file. $!";
}
}
close $fh or die "Unable to close $file. $!";

print "Processing file: $file\n";
process(%data);

push @matrix, \%data;
}

for my $i (0 .. $#matrix) {
for my $j ($i+1 .. $#matrix) {
print "Combining $file[$i] and $file[$j]\n";
my %data = combine($matrix[$i], $matrix[$j]);
process(%data);
}
}

sub process {
my %data = @_;
my %seen;

my @product = sort {$a <=> $b}
grep ! $seen{$_}++,
map keys %$_, values %data;

# to get column width for print
my $wid = 1 + max map length, @product;

printf "%7s" . "%${wid}s" x @product . "\n", 'prod->', @product;

for my $substrate (sort {$a <=> $b} keys %data) {
printf "%7s", $substrate;
printf "%${wid}s", $data{$substrate}{$_} || '-' for @product;
print "\n";
}
printf "\n%5s\n%5s\n%s\n\n", '^', '|', 'substrate';
}


sub combine {
my ($matrix1, $matrix2) = @_;
my %new_hash = %$matrix1;

for my $substrate (keys %$matrix2) {
$new_hash{$substrate}{$_} = 1 for keys %{ $matrix2->{$substrate} };
}
return %new_hash;
}


I've attached 2 files. One has the three data files and the other has the output. And now a third file, using the 'SUPERLIST_PRODUCT.txt' AND 'SUPERLIST_SUBSTRATE.txt


(This post was edited by Chris Charley on Sep 4, 2012, 4:46 PM)
Attachments: FILE.txt (1.71 KB)
  matrices.txt (15.5 KB)
  t33.pl (2.12 KB)


rushadrena
Novice

Sep 7, 2012, 1:05 AM

Post #30 of 62 (5779 views)
Re: [Chris Charley] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Hi Chris,
I have 10 directories and each of them has a file "list".
I modified your code to create pairwise concatenated matrices but Im getting this error --"No such file or directory at code2.pl line 6", whereas the file does exist.
Here's the code :-

Code
#!/usr/bin/perl 
use strict;
use warnings;
use List::Util qw/ max /;

open my $fh, "<", 'SUPERLIST_PRODUCT' or die $!;
my $spr_prod = do {local $/; <$fh>};
close $fh or die $!;
my @spr_prod = $spr_prod =~ /\d+/g;

open $fh, "<", 'SUPERLIST_SUBSTRATE' or die $!;
my $spr_substr = do {local $/; <$fh>};
close $fh or die $!;
my @spr_substrate = $spr_substr =~ /\d+/g;
my $i=0;
my $j=0;
my @matrix;
my @file;
my @dirs = split /\n/,`find -maxdepth 1 -type d`;
for ($i=1;$i<=10;$i++)
{
for ($j=1;$j<=10;$j++)

{

my $path = '.'; # (current directory - '.') or path to data files
my @file = qw($dirs[$i]/list $dirs[$j]/list);

for my $file ( @file ) {
my %data;
my @substrate;

open my $fh, "<", "$path/$file"
or die "Unable to open $file for reading. $!";

while (<$fh>) {
if (/^substrate/) {
@substrate = /\d+/g;
}
elsif (/^product/) {
while (/(\d+)/g) {
for my $sub (@substrate) {
$data{$sub}{$1} = 1 ;
}
}
}
else {
die "Unknown format $file. $!";
}
}
close $fh or die "Unable to close $file. $!";

print "Processing file: $file\n";
process(\@spr_prod, \@spr_substrate, %data);

push @matrix, \%data;
}
}}
for my $i (0 .. $#matrix) {
for my $j ($i+1 .. $#matrix) {
print "Combining $file[$i] and $file[$j]\n";
my %data = combine($matrix[$i], $matrix[$j]);
process(\@spr_prod, \@spr_substrate, %data);
}
}

sub process {
my ($spr_prod, $spr_subst, %data) = @_;
my %seen;

my @product = sort {$a <=> $b}
grep ! $seen{$_}++,
@$spr_prod, map keys %$_, values %data;

# to get column width for print
my $wid = 1 + max map length, @product;

printf "%7s" . "%${wid}s" x @product . "\n", 'prod->', @product;

undef %seen;
my @substrate = sort {$a <=> $b}
grep ! $seen{$_}++,
@$spr_subst, keys %data;

for my $substrate (@substrate) {
printf "%7s", $substrate;
printf "%${wid}s", $data{$substrate}{$_} || '-' for @product;
print "\n";
}
printf "\n%5s\n%5s\n%s\n\n", '^', '|', 'substrate';
}


sub combine {
my ($matrix1, $matrix2) = @_;
my %new_hash = %$matrix1;

for my $substrate (keys %$matrix2) {
$new_hash{$substrate}{$_} = 1 for keys %{ $matrix2->{$substrate} };
}
return %new_hash;
}



(This post was edited by rushadrena on Sep 8, 2012, 3:24 AM)


Laurent_R
Veteran / Moderator

Sep 7, 2012, 4:43 AM

Post #31 of 62 (5771 views)
Re: [rushadrena] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Does the file exist in the directory from where you run your program? Or is it in a subdirectory?


rushadrena
Novice

Sep 7, 2012, 9:06 AM

Post #32 of 62 (5748 views)
Re: [Laurent_R] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Yup Laurent. The SUPERLIST_PRODUCT & SUPERLIST_SUBSTRATE files exist in the present directory, which further has 10 subdirectories. And each of these subdirectories then have a "list" file.


Laurent_R
Veteran / Moderator

Sep 7, 2012, 10:16 AM

Post #33 of 62 (5744 views)
Re: [rushadrena] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Can you post the result of a 'ls -al' command in your directory?


rushadrena
Novice

Sep 7, 2012, 12:03 PM

Post #34 of 62 (5737 views)
Re: [Laurent_R] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Here it is :-
ls -al


Code
drwxr-xr-x 3 vanilla vanilla 4096 2012-09-05 22:35 acs/ 
-rw-r--r-- 1 vanilla vanilla 2215 2012-09-07 13:20 code2.pl
drwxr-xr-x 2 vanilla vanilla 4096 2012-09-05 20:32 dre/
drwxr-xr-x 2 vanilla vanilla 4096 2012-09-05 20:33 gga/
drwxr-xr-x 2 vanilla vanilla 4096 2012-09-05 20:33 mdo/
drwxr-xr-x 2 vanilla vanilla 4096 2012-09-05 20:34 mgp/
drwxr-xr-x 2 vanilla vanilla 4096 2012-09-05 20:34 oaa/
drwxr-xr-x 2 vanilla vanilla 4096 2012-09-05 20:38 spu/
drwxr-xr-x 2 vanilla vanilla 4096 2012-09-05 20:38 ssc/
-rw-r--r-- 1 vanilla vanilla 5884 2012-09-05 20:51 SUPERLIST_PRODUCT
-rw-r--r-- 1 vanilla vanilla 5884 2012-09-05 20:58 SUPERLIST_SUBSTRATE
drwxr-xr-x 2 vanilla vanilla 4096 2012-09-05 20:47 xla/
drwxr-xr-x 2 vanilla vanilla 4096 2012-09-05 20:46 xtr/



Laurent_R
Veteran / Moderator

Sep 8, 2012, 1:36 AM

Post #35 of 62 (5691 views)
Re: [rushadrena] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Assuming code2.pl is your program and that you are launching it from the directory, everything seems OK, the file permissions should enable you to open the 2 SUPERLIST files.

I do not understand why opening these files should fail. Can you view your SUPERLIST files with the cat or more commands? I'm asking because there is sometimes an invisible or hidden character in file names.


rushadrena
Novice

Sep 8, 2012, 3:27 AM

Post #36 of 62 (5682 views)
Re: [Laurent_R] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Ohh my bad. Actually there was a typo in file name I removed it and the code has now moved to next 32 lines. With an error for line 33 as :-

Code
Unable to open $dir[$i]/list for reading. No such file or directory at code2.pl line 33.


Am I reading the files wrongly at line number 33.


Laurent_R
Veteran / Moderator

Sep 8, 2012, 7:10 AM

Post #37 of 62 (5675 views)
Re: [rushadrena] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Hi,

I think that the command:


Code
my @file = qw($dirs[$i]/list $dirs[$j]/list);


probably does not do what you seem to think it does, although I am not entirely sure of what you are trying to do with it. Add a print on @file or use the debugger to figure out the contents of the @file array.

If you want a list of files in directory dirs[$i], you may want to use the glob function or you could do something like this:


Code
my $file_list = `ls $dirs[$i]`; 
my @files = split /\n/, $file_list;



Chris Charley
User

Sep 8, 2012, 9:27 AM

Post #38 of 62 (5655 views)
Re: [rushadrena] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

The way you have set up the for loops is wrong, (I'll show below), and does not interface with the rest of the program. I believe you want the pairings of each matrix, (and not the matrix by itself). That seems to be the intention of your attempt to solve the problem.

You also have other issues which you have been discussing with Laurent. How to find the files, etc.

To find all pairs, I set up a table from 1 to 5, (instead of 1 to 10), just to demonstrate combinations.


Code
 #!/usr/bin/perl  
use strict;
use warnings;

for (my $i=1;$i<=5;$i++) {
for (my $j=1;$j<=5;$j++) {
print "($i $j)\n";
}
}
print "\ncombinations\n";
for (my $i=1;$i<=5;$i++) {
for (my $j=$i+1;$j<=5;$j++) {
print "($i $j)\n";
}
}
__END__
C:\Old_Data\perlp>perl t7.pl
(1 1) duplicate
(1 2)
(1 3)
(1 4)
(1 5)
(2 1) done earlier (1 2)
(2 2) duplicate
(2 3)
(2 4)
(2 5)
(3 1) done earlier (1 3)
(3 2) done earlier (2 3)
(3 3) duplicate
(3 4)
(3 5)
(4 1) done earlier (1 4)
(4 2) done earlier (2 4)
(4 3) done earlier (3 4)
(4 4) duplicate
(4 5)
(5 1) done earlier (1 5)
(5 2) done earlier (2 5)
(5 3) done earlier (3 5)
(5 4) done earlier (4 5)
(5 5) duplicate

combinations
(1 2)
(1 3)
(1 4)
(1 5)
(2 3)
(2 4)
(2 5)
(3 4)
(3 5)
(4 5)

C:\Old_Data\perlp>


You can see in the first column that some pairs have already been seen, just in reverse order, (1 2) and (2 1) for example.

Note: arrays in Perl begin with 0 and not 1.


(This post was edited by Chris Charley on Sep 8, 2012, 11:25 AM)


Laurent_R
Veteran / Moderator

Sep 8, 2012, 11:04 AM

Post #39 of 62 (5650 views)
Re: [Chris Charley] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Right, Chris, I had not seen this error un the OP's code.

To have only the combinations, you could also do something like that:


Code
my @file_counters = 1..5; 
while (my $i = shift @file_counters){
print "$i $_ \n" foreach @file_counters;
}


which prints:
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5


rushadrena
Novice

Sep 8, 2012, 1:32 PM

Post #40 of 62 (5641 views)
Re: [Laurent_R] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Hi Laurent. I dont want to list all the files in each of the ten directories. "list" is actually the name of the file. "list" is the only file I'm concerned about (the file "list" resides in each of the ten directories). The code will select first directory and then takes its "list" file,generate matrix and concatenate it with the "list" file generated matrix of second directory. Pairwise matrices ,that is.

Code
1->1 
1->2
1->3
1->4
.....
2->1
2->2
......


The debug mode outputs ::-

Code
main::(code2.pl:26):	my $path = '.'; # (current directory - '.') or path to data files 
DB<1> n
main::(code2.pl:27): my @file = qw($dirs[$i]/list $dirs[$j]/list);
DB<1> n
main::(code2.pl:29): for my $file ( @file ) {
DB<1> n
main::(code2.pl:30): my %data;
DB<1> n
main::(code2.pl:31): my @substrate;
DB<1> n
main::(code2.pl:33): open my $fh, "<", "$file"
main::(code2.pl:34): or die "Unable to open $file for reading. $!";
DB<1> print @dirs
../xla./mgp./oaa./acs./spu./mdo./ssc./dre./xtr./gga
DB<2> print $dirs[1]
./xla



(This post was edited by rushadrena on Sep 8, 2012, 1:33 PM)


Laurent_R
Veteran / Moderator

Sep 9, 2012, 12:36 AM

Post #41 of 62 (5603 views)
Re: [rushadrena] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Allright, I misunderstood what you were doing.

I do not understand, your error message does not correspond to your code.

The error message:


Code
Unable to open $dir[$i]/list


In the previous code, your list of directories is @dirs, not @dir.

But the code you had submitted said the directory is $path, which seems to be the current path.

It seems from de debugging sessions you've juste posted that you've corrected these errors, do you still have an error message when opening the file?

On another point, I don't see the interest of comparing 1 with 1, and also probably not 1 with 2 and 2 with 1.


rushadrena
Novice

Sep 9, 2012, 2:26 AM

Post #42 of 62 (5599 views)
Re: [Laurent_R] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Laurent, see the problem is that the values of dirs (i.e. the @dirs) are getting displayed(as shown by the debug code), but the file "list" inside each of these directories are not being read. So Im still getting this error :-

Code
Unable to open $dirs[$i]/list for reading. No such file or directory at code2.pl line 33.


And yes , there aint no point in comparing 1 with 1 (and so on), that was just to explain you the problem scenario.


Laurent_R
Veteran / Moderator

Sep 9, 2012, 2:36 AM

Post #43 of 62 (5596 views)
Re: [rushadrena] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

 
Don't use qw but qq to create your list of files.

qw/foo bar . . ./ is the same as (’foo’,’bar’,. . .)

Variables are not interpolated in this type of construct.

With qq/$dirs[$i] .../, it should work, since variables will be interpolated (and escape sequences will be processed).


rushadrena
Novice

Sep 9, 2012, 4:17 AM

Post #44 of 62 (5590 views)
Re: [Laurent_R] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Laurent that was a good point. So here's the relevant snippet from my code.

Code
my $path = '.'; # (current directory - '.') or path to data files 
my @file = qq($dirs[$i]/list $dirs[$j]/list );

for my $file ( @file ) {
my %data;
my @substrate;

open my $fh, "<", "$file"
or die "Unable to open $file for reading. $!";

Now the file is being read but there's another error

Code
 Unable to open ./xla/list ./xla/list  for reading. No such file or directory at code2.pl line 35.


Debugging portion :-

Code
main::(code2.pl:27):	my $path = '.'; # (current directory - '.') or path to data files 
DB<1> n
main::(code2.pl:29): my @file = qq($dirs[$i]/list $dirs[$j]/list );
DB<1> n
main::(code2.pl:31): for my $file ( @file ) {
DB<1> n
main::(code2.pl:32): my %data;
DB<1> n
main::(code2.pl:33): my @substrate;
DB<1> n
main::(code2.pl:35): open my $fh, "<", "$file"
main::(code2.pl:36): or die "Unable to open $file for reading. $!";
DB<1> n
Unable to open ./xla/list ./xla/list for reading. No such file or directory at code2.pl line 35.
Debugged program terminated. Use q to quit or R to restart,

DB<2> print @file
./xla/list ./xla/list
DB<3> print $file[0]
./xla/list ./xla/list
DB<4> print $file[1]


The @file should be holding ./xla/list as $file[0] and the second file ./xla/list as $file[1].


Laurent_R
Veteran / Moderator

Sep 9, 2012, 5:56 AM

Post #45 of 62 (5578 views)
Re: [rushadrena] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Sorry, my error. There is an improvement, since the variables are now interpolated, but the two files names get stored in the first element of the array, because qq// makes a single string, not a list of strings.

Try this instead:


Code
my @file = ($dirs[$i]/list, $dirs[$j]/list );


Actually, you could also open directly "$dirs[$i]/list" and $dirs[$j]/list, but if you find it more convenient to create a temporary array with the two files, then the syntax above should work.


rushadrena
Novice

Sep 9, 2012, 8:13 AM

Post #46 of 62 (5572 views)
Re: [Laurent_R] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Tried this

Code
my @file = ($dirs[$i]/list, $dirs[$j]/list );

But got this error

Code
Bareword "list" not allowed while "strict subs" in use at code2.pl line 29. 
Bareword "list" not allowed while "strict subs" in use at code2.pl line 29.
Execution of code2.pl aborted due to compilation errors.



Laurent_R
Veteran / Moderator

Sep 9, 2012, 9:13 AM

Post #47 of 62 (5563 views)
Re: [rushadrena] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Sorry, again an error. Should be:


Code
my @file = ("$dirs[$i]/list", "$dirs[$j]/list" );



rushadrena
Novice

Sep 10, 2012, 7:24 AM

Post #48 of 62 (5531 views)
Re: [Laurent_R] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Hi Laurent.
Finally the code is running fine,though the output file is huge,but that's not an issue as the format is what I wanted.
But a strange error keeps popping up after 10 minutes of running the code ( a complete run for 10X10 would take around 20 minutes)

Code
$ perl code2.pl >> 10_X_10.txt 
Use of uninitialized value within @file in concatenation (.) or string at code2. pl line 63.
Use of uninitialized value in concatenation (.) or string at code2.pl line 63.



Laurent_R
Veteran / Moderator

Sep 10, 2012, 8:12 AM

Post #49 of 62 (5512 views)
Re: [rushadrena] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Please tell what is line 63 of your code.


rushadrena
Novice

Sep 10, 2012, 8:17 AM

Post #50 of 62 (5510 views)
Re: [Laurent_R] A file parsing and 2D array/matrix problem. [In reply to] Can't Post

Here it is

Code
61 for my $i (0 .. $#matrix) { 
62 for my $j ($i+1 .. $#matrix) {
63 print "Processing $file[$i] and $file[$j]\n";
64 my %data = combine($matrix[$i], $matrix[$j]);
65 process(\@spr_prod, \@spr_substrate, %data);
66 }
67 }


First page Previous page 1 2 3 Next page Last page  View All
 
 


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

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