Note that there are some explanatory texts on larger screens.

plurals
  1. POlooping in a folder reading files with a specific pattern...getting an error perl
    primarykey
    data
    text
    <p>I have a code that is able to read two files as input and make a comparison between the two writing matching elements in the output. Let's consider the $list_file readen by my script as a fixed list used to chose elements from the $data_file. I'm trying to make this script looping in a folder and reading multiple data_files with a specific name pattern, but I'm getting an error and I'm not able to solve it.</p> <p>Here is my list_file named "list.txt":</p> <pre><code>X1 A B X2 C D X3 E F </code></pre> <p>And my first data_file named "data_file1.txt":</p> <pre><code>A X1 2 5 B X1 3 7 C X2 1 4 D X2 1 5 </code></pre> <p>And my second data_file named "data_file2.txt":</p> <pre><code>E X3 5 7 F X3 3 4 G X4 2 3 H X4 2 5 </code></pre> <p>I would like to obtain an output that looks like this:</p> <pre><code>X1 A B 2 5 3 7 X2 C D 1 4 1 5 X3 E F 5 7 3 4 </code></pre> <p>Both my data_files are in a folder that contains also other files, so I need to match "data" as pattern to recognize the correct inputs.</p> <p>And this is my code:</p> <pre><code>my $list_file = "list.txt"; my $dirname = "data_directory"; my $dh; use strict; use warnings; use autodie; use feature 'say'; opendir ($dh, $dirname) || die "Impossible open the $dirname!"; while (my $data_file = readdir ($dh)){ if ("$dh/$data_file" =~ /data/){ open my $data_fh, "&lt;", $data_file; my %data; while (&lt;$data_fh&gt;) { chomp; my ($id2, $id1, @data) = split /\t/; $data{$id1}{$id2} = \@data; } open my $list_fh, "&lt;", $list_file; LINE: while(&lt;$list_fh&gt;) { chomp; my ($id1, @id2s) = split /\t/; my $data_id1 = $data{$id1}; defined $data_id1 or next LINE; my @values = map @{ $data_id1-&gt;{$_} }, @id2s; say join "\t", $id1, @id2s, @values; } } } closedir (DIR); </code></pre> <p>If I run this code I get the right result only for the first data_file. Moreover I get this error:</p> <pre><code>"Can't open 'data_file2.txt' for reading: 'No such file or directory' at code.pl line 23" </code></pre> <p>Suggestions are welcome!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload