Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you're looking for a list of all files whose names start with <code>data_</code> and end with <code>.txt</code>, and they're guaranteed to be in a single level of the directory tree that's immediately below the script's working directory, you can cheaply obtain the list with</p> <pre><code>@data_files = glob('**/data_*.txt'); </code></pre> <p>If they're in a single directory, e.g. "datapath", that's even simpler; just</p> <pre><code>@data_files = glob('datapath/data_*.txt'); </code></pre> <p>In general, this is a problem you don't have to solve for yourself, because the Perl implementers have already solved it for you; for more information on how to adapt <code>glob()</code> to your specific case, try <code>perldoc -f glob</code>, which will give you all the detail you can stand. Failing that, you might look into <code>File::Find</code>, which requires a bit more effort, but offers much more flexibility in exchange; for problems <code>glob()</code> can't solve, <code>File::Find</code> should probably be the first tool you reach for.</p> <p>In this case, though, assuming the following directory structure</p> <pre><code>list-and-data/ list-and-data/list.txt list-and-data/data/data_1.txt list-and-data/data/data_2.txt list-and-data/data/et-cetera.txt list-and-data/data/something-else.txt </code></pre> <p>and that you want only to pick out the data files in <code>data/</code>, you might simply:</p> <pre><code>sub read_file { my $filename = shift(); my @data; open my $fh, '&lt;', $filename or die "Can't open '$filename' to read: $!\n"; @data = &lt;$fh&gt; or die "Failed to read '$filename', or empty: $!\n"; close $fh; chomp foreach @data; return \@data; }; my $list_file = "list.txt"; my $dirname = "data/"; my $data_filespec = "data_*.txt"; my @list = @{ read_file($list_file) }; my %data = {}; my @files = glob("$dirname/$data_filespec"); foreach my $file (@files) { my $basename = $file; $basename =~ s@^.*/@@; $data{$basename} = read_file($file); }; </code></pre> <p>At this point, you have the contents of <code>list.txt</code> in <code>@list</code>, and the contents of all your data files in <code>%data</code>, keyed by the file's basename (e.g. "list-and-data/data/data_1.txt" would be keyed by "data_1.txt").</p> <p>This simplifies your life considerably; there's no need to bother with <code>opendir()</code> and friends, and your file-reading code is all in one place, so if you need to make any changes to how it behaves, you won't have to wander through the entire source file doing the same thing over and over.</p> <p>Similarly, all your data is in one place, helpfully keyed by the filename in which it was found, so the remainder of the analysis is as simple as iterating over <code>keys %data</code>.</p>
 

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