in reply to Re^2: Repeated Pattern Matching Fails
in thread Repeated Pattern Matching Fails
You don't need a pattern for that, split will do:
use Data::Dumper; my %found; while (<DATA>) { my ($file, @vers) = split " "; if (-f $file) { $found{$file} = [ @vers ]; } } print Dumper(\%found); __DATA__ Files Checked IN: test/abc.txt 1.23 1.3.4.5 team/hello.cpp 2.1 NONE Clear/thing.pl NONE 1.2.34 etc etc
Output:
$VAR1 = { 'Clear/thing.pl' => [ 'NONE', '1.2.34' ], 'team/hello.cpp' => [ '2.1', 'NONE' ], 'test/abc.txt' => [ '1.23', '1.3.4.5' ] };
You write
Need to call this pattern in one line..(requirement of my script)
Do you mind to tell why you do have such a requirement?
<update>
Anyways, you want the /g modifier on your pattern, and thus you can't say
if ( $var =~ m{$pattern}g ) {
because that will match only once (but without resetting the match location). If you really have to match globally and evaluate the result in a scalar (or boolean) context, you have to write something like
if ( (@matches = $var =~ m{$pattern}g ) > 0) {
which is ugly and not very readable. Anyways, here it is...
$var="Files Checked IN: test/abc.txt 1.23 1.3.4.5 team/hello.cpp 2.1 NONE Clear/thing.pl NONE 1.2.34 etc etc"; my $pattern = '([\w./]+)\s+([\d.]+|NONE)\s+([\d.]+|NONE)'; if ( ( @matches = $var =~ m{$pattern}g ) >0 ) { print join('|',@matches),$/; }
</update>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Repeated Pattern Matching Fails
by kapila (Acolyte) on Apr 15, 2009 at 08:51 UTC | |
by parv (Parson) on Apr 15, 2009 at 10:10 UTC |