I am trying to use Perl to extract fields in a csv file I exported from Excel...
#!/usr/bin/perl
use strict;
use warnings;
our $list;
our @clients;
our $filedef1=$ARGV[0]; #name of client CSV file
&read_clients ();
# define regex components
my $accode = qr(^"(.*)",.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*)x;
my $name = qr(^.*,"(.*)",.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*)x;
# do regex matches
print "Extractions:\n";
my @extractions = $list =~ m{(?: $name)}mxgc;
print "$extractions[$_], " for 0.. $#extractions;
print "End of Program!\n";
##Beginning of subroutine for reading the document source file.
sub read_clients
{
open FILEDEF1, "< $filedef1" or die "error reading $filedef1-$!";
while (<>)
{
push (@clients, <FILEDEF1>);
}
close FILEDEF1;
$list = join(' ',@clients);
print $list;
} ##End of block for reading the document source file.
This code works like I want it to. When I substitute $accode for $name in the line
my @extractions = $list =~ m{(?: $name)}mxgc;
It only extracts the first record/line match for $accode, while I get every record/line match if I do a separate run and match for $name instead. In other words, I am not getting global, multiline matching on $accode while I am on $name. Any idea why? I want every record match for $accode.
-
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.