in reply to Re: Repeated Pattern Matching Fails
in thread Repeated Pattern Matching Fails

Thanks Beth & Parv for your replies. Beth thanks for correcting me in my pattern. I basically require a pattern wherein, it matches the lines-

$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";
o/p should be- each file(e.g test/abc.txt ), each revision should come in variables $1,$2 etc i.e matches How can i call the same pattern number of times? Need to call this pattern in one line..(requirement of my script)

Replies are listed 'Best First'.
Re^3: Repeated Pattern Matching Fails
by shmem (Chancellor) on Apr 14, 2009 at 12:11 UTC

    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>

        Emphasis is mine ...

        Thanks shmem,beth, parv,vivek ... [g]od bless you all ...

        *groan*   In that case I would have preferred not to be mentioned.