G'day biohak,
Welcome to the monastery.
Firstly, please don't alter your OP without indicating what you've changed.
See "How do I change/delete my post?" for an explanation of why not and how to indicate changes.
The code you currently have posted has many issues:
-
"RECORD: foreach my $filenames (@filenames) {": The label is unnecessary (as ++LanX has pointed out) and each element of @filenames is one filename so let the variable reflect this, i.e. $filename.
-
"chomp $XML_infile;": This appears to be doing nothing: see chomp.
-
"open INDATA, "C:/_OUTPUT/$filenames" or die "INDATA: $! \n\n ";": Use the 3-argument form of open with a lexical filehandle; you already have the file path in $XML_infile so use it here; your die message should contain a filename (INDATA is a filehandle) — alternatively, use the autodie pragma.
-
"while (!eof INDATA ) {": There's no need for eof here, just read from the filehandle (optionally assigning to a variable). See "perlsyn: Loop Control".
-
"my $line = <INDATA>;": See last point.
-
"if ( $line =~ /\"PlateID\"/g ) {": The 'g' modifier is for repeat matching which isn't what you're doing here. See "perlre: Modifiers".
-
"$outfile= ">C:/_OUTPUT/_DATA/$filenames" . $plate_id . ".txt";": You haven't assigned a value to $plate_id.
-
"next RECORD;": last is what you need here (as LanX has pointed out).
-
"elsif () {#pull data for PlateID}": It looks like that should be else {...}. See "perlsyn: Compound Statements".
-
"elsif () {#Process rest of file }": This looks unnecessary: I see no need for either elsif or else here.
Using this dummy data (as I've no idea what your real data looks like):
$ cat pm_1077685_A.txt
Line 1: no plate info
Line 2: ... PlateID 123 ...
Line 3: ... PlateID 456 ...
Line 4: no plate info
$ cat pm_1077685_B.txt
Line 1: no plate info
Line 2: ... PlateID 789 ...
Line 3: ... PlateID ...
Line 4: no plate info
$ cat pm_1077685_C.txt
Line 1: no plate info
Line 2: ... PlateID 000 ...
Line 4: no plate info
This script shows similar processing to what you appear to be attempting and includes examples of the points I've made above.
#!/usr/bin/env perl
use strict;
use warnings;
use autodie;
my @filenames = qw{pm_1077685_A.txt pm_1077685_B.txt pm_1077685_C.txt}
+;
for my $filename (@filenames) {
my $in_path = "./$filename";
print "Processing: '$in_path'\n";
open my $in_fh, '<', $in_path;
while (<$in_fh>) {
print "\t$_";
if (/PlateID/) {
if (/PlateID (\d+)/) {
my $plate_id = $1;
print "\t\tProcess PlateID '$plate_id' from '$in_path'
+\n";
}
else {
print "\t\tpopup_error_window($in_path)\n";
last;
}
}
}
}
Output:
$ pm_example.pl
Processing: './pm_1077685_A.txt'
Line 1: no plate info
Line 2: ... PlateID 123 ...
Process PlateID '123' from './pm_1077685_A.txt'
Line 3: ... PlateID 456 ...
Process PlateID '456' from './pm_1077685_A.txt'
Line 4: no plate info
Processing: './pm_1077685_B.txt'
Line 1: no plate info
Line 2: ... PlateID 789 ...
Process PlateID '789' from './pm_1077685_B.txt'
Line 3: ... PlateID ...
popup_error_window(./pm_1077685_B.txt)
Processing: './pm_1077685_C.txt'
Line 1: no plate info
Line 2: ... PlateID 000 ...
Process PlateID '000' from './pm_1077685_C.txt'
Line 4: no plate info
|