Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Opening multiple files in directory

by Athanasius (Archbishop)
on Aug 31, 2014 at 16:50 UTC ( [id://1099127]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Opening multiple files in directory
in thread Opening multiple files in directory

I haven’t tried to run this code, but just looking through it there are two obvious problems:

  1. Within a regex, a dot matches any character other than a newline (unless the regex has an /s modifier). So /txt.fit/ matches “mytxt.fit”, but it also matches “yourtxtafit”, “histxt0fitandmore”, etc. You need to backslash the . to make it match a literal dot only: /txt\.fit/; and unless “fit” may be followed by other characters, you want: /txt\.fit$/.

  2. $tree is a scalar variable. Within the inner while loop, each time the line $tree = $_; is executed, it overwrites whatever was in the variable. So when this inner loop finishes, only the last line containing “Tree mixture” is written to the output file. Here is one way to correct this (untested):

    foreach my $file (@files) { my $outfile = "$file.tre"; open(IN, '<', $file) or die $!; open(FILE, '>>', $outfile) or die $!; while (my $tree = <IN>) { next unless /Tree mixture/; $tree =~ s/Tree mixtureTree=//; print FILE $tree; } close FILE or die $!; close IN or die $!; }

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1099127]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-29 06:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found