I think you want something like this (untested):
my $dir = 'C:/Documents and Settings/mydir/Desktop/current/Test_Files'
+;
open my $out, '>', "$dir/data.txt" or die "can't open out file: $!";
opendir my $dh, $dir or die "can't opendir $dir : $!";
while(my $f = readdir($dh)) {
next if ($f eq 'data.txt');
open my $fh, '<', "$dir/$f" or die "can't open file $f : $!";
my $first_line = <$fh>;
while (my $line = <$fh>) {
chomp $line;
my ($well,$sample,$barcode,$block_id) = split(/\t/, $line);
my $name = substr($block_id, 11);
$sample =~ /(\d+)(.*)/;
print $outfile "$well\t$1\t$2\$barcode\t$name\n";
}
close $fh;
}
closedir $dh;
close $out;
Notes:
- readdir lets you loop over all the files in a directory, but you still need to open and read each file.
- It's not good to write your output into the same place all your input files are - your script will try to read it too!
- Be careful using the special variable $_ - this is the "default" output of many operations, so it's easy for one to clobber another. I like to store lines read from a file into a variable, just to be safe.
- The special variables $1 and $2 (regex matches) are also easy to clobber (what if you add another regex to your script sometime in the future?) So either store them in other variables right after the regex, or else use them immediately.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.