http://qs321.pair.com?node_id=11113532


in reply to Re^2: Regular Expression to Parse Data from a PDF
in thread Regular Expression to Parse Data from a PDF

'They might add a "R-1" or "R-2" to the far left column if there is a revision.'

You just need to extend the regex to handle that. Here's an example:

#!/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);

Output:

$VAR1 = [ [ '777', '', '111' ], [ '777', 'A', '222' ], [ '777R-1', '', '333' ], [ '777R-2', 'A', '444' ] ];
print ... $fields[1] . ",". $fields[3] . ",". $fields[4] . ",". ...

Here's an example to show a better way to handle that:

$ perl -e 'my @x = qw{a b c d e f}; print join ",", @x[0,3,4]' a,d,e

On an unrelated note, there are problems with your 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 "Can't open the output file ...". Look to using lexical filehandles and the 3-argument form of open. Consider the autodie pragma — you'll do less work and get better error reporting.

— Ken

Replies are listed 'Best First'.
Re^4: Regular Expression to Parse Data from a PDF
by kevyt (Scribe) on Feb 28, 2020 at 06:52 UTC
    Thanks Ken! I will read this in the morning. It's 1:51 AM here :) The goal is to help someone determine which bids they are losing and manually doing it is very time consuming. Thanks! Kevin