in reply to Regexp mystery (to me)
This code works like I want it to.
It does? Really? OK.
#!/usr/bin/perl
use strict;
use warnings;
our $list;
our @clients;
our $filedef1=$ARGV[0]; #name of client CSV file
Why are you declaring those variables here when you are only using them inside the read_clients() subroutine?
&read_clients ();
You shouldn't use & when calling subroutines, see perlsub for reasons why.
# define regex components
my $accode = qr(^"(.*)",.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*)x;
my $name = qr(^.*,"(.*)",.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*)x;
Why include the empty fields after the captured field?
# do regex matches
print "Extractions:\n";
my @extractions = $list =~ m{(?: $name)}mxgc;
Why are you using the /c option? It is only relevant if you are using the \G zero-width assertion in the pattern.
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 (<>)
The special <> readline operator will treat @ARGV as a list of file names and open and read each line from all of those files. Since $filedef1 is the first element of @ARGV the file will be opened and the first line from that file will be read into the $_ variable.
{
push (@clients, <FILEDEF1>);
You are pushing all the lines from the file onto the @clients array from inside the loop so you should have the number of lines times the file in the array.
}
close FILEDEF1;
$list = join(' ',@clients);
You are joining the lines together with a single space character.
print $list;
} ##End of block for reading the document source file.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Regexp mystery (to me)
by ysth (Canon) on Mar 03, 2008 at 21:34 UTC | |
by jwkrahn (Abbot) on Mar 03, 2008 at 23:27 UTC | |
Re^2: Regexp mystery (to me)
by barkingdoggy (Initiate) on Mar 03, 2008 at 21:00 UTC | |
by dragonchild (Archbishop) on Mar 03, 2008 at 21:27 UTC |
In Section
Seekers of Perl Wisdom