note
kcott
<blockquote>
<em>'They might add a "R-1" or "R-2" to the far left column if there is a revision.'</em>
</blockquote>
<p>
You just need to extend the regex to handle that. Here's an example:
</p>
<code>
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
my $jid = '777';
my $text = 'header 777 111 777 A 222 777R-1 333 777R-2 A 444';
my $re = qr{(?x: \A (R-\d+|) \s*? (A|) \s (\d+) )};
my @lines;
my $wanted_line = 0;
for my $line (split /$jid/, $text) {
next unless $wanted_line++;
my @fields = $line =~ $re;
push @lines, [ $jid . shift(@fields), @fields ];
}
print Dumper(\@lines);
</code>
<p>Output:</p>
<code>
$VAR1 = [
[
'777',
'',
'111'
],
[
'777',
'A',
'222'
],
[
'777R-1',
'',
'333'
],
[
'777R-2',
'A',
'444'
]
];
</code>
<blockquote>
<em><c>print ... $fields[1] . ",". $fields[3] . ",". $fields[4] . ",". ...</c></em>
</blockquote>
<p>
Here's an example to show a better way to handle that:
</p>
<code>
$ perl -e 'my @x = qw{a b c d e f}; print join ",", @x[0,3,4]'
a,d,e
</code>
<p>
On an unrelated note, there are problems with your [https://perldoc.perl.org/5.30.0/functions/open.html|open] statements.
Use of package variables can lead to all sorts of bugs that are hard to track down.
Your six error messages are identical: how will you know which file generates <em>"Can't open the output file ..."</em>.
Look to using lexical filehandles and the 3-argument form of [https://perldoc.perl.org/5.30.0/functions/open.html|open].
Consider the [https://perldoc.perl.org/5.30.0/autodie.html|autodie] pragma —
you'll do less work and get better error reporting.
</p>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-861371">
<p>— Ken</p>
</div></div>
11113472
11113527