Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
'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


In reply to Re^3: Regular Expression to Parse Data from a PDF by kcott
in thread Regular Expression to Parse Data from a PDF by kevyt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-23 19:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found