sravs448 has asked for the wisdom of the Perl Monks concerning the following question:

I want to split the specific words in "result" varaible which comes after "transaction","user:" and "#" .I could achieve it through the below code. But at times there wont be the line start with "#"(Its an optional one). I am not able to handle such cases (when $3 is blank)

foreach my $number(@transaction_numbers) { my $result =`command to get result`; print $result; while ($result =~ /transaction (\d+).*user:\s*(\S+)\s*#\s*(.*)/g) +{ my ($transaction, $user, $comment) = ($1, $2, $3); printf "%-12s %-10s %s\n", $1, $2, $3; } }
The code works fine with the 1st transaction, but shows prints nothing for 2nd one

output of $result is :

transaction 1234567; promote; 2014/08/01 10:22:37 ; user: john # Performing a "promote" on file. /./abc/desf/test.pl 138699/1 (138700/1) transaction 4578643; promote; 2014/08/01 10:22:37 ; user: sam /./abc/desf/test.pl 138699/1 (138700/1)

Curent output is

Transaction User Comments 1234567 john Performing a "promote" on file.

Desired output is

Transaction User Comments 1234567 john Performing a "promote" on file. 4578643 sam

Replies are listed 'Best First'.
Re: Reg the split expression
by kennethk (Abbot) on Aug 04, 2014 at 20:29 UTC
    You should be able to get your desired result with Non capturing groupings:
    use strict; use warnings; my $result = <<'EOT'; transaction 1234567; promote; 2014/08/01 10:22:37 ; user: john # Performing a "promote" on file. /./abc/desf/test.pl 138699/1 (138700/1) transaction 4578643; promote; 2014/08/01 10:22:37 ; user: sam /./abc/desf/test.pl 138699/1 (138700/1) EOT while ($result =~ /transaction (\d+).*user:\s*(\S+)\s*(?:#\s*(.*))?/g) + { my ($transaction, $user, $comment) = ($1, $2, $3); printf "%-12s %-10s %s\n", $1, $2, $3//''; }

    Note the // (Logical Defined Or) to avoid an undef warning in your print.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      The  // defined-or operator was introduced in Perl version 5.10, which also introduced the  (?|pattern) "branch reset" extended pattern (see Extended Patterns in perlre), which can be used to similar effect.

      use 5.010; # need (?|pattern) "branch reset" regex construct use strict; use warnings; my $result = <<'EOT'; transaction 1234567; promote; 2014/08/01 10:22:37 ; user: john # Performing a "promote" on file. /./abc/desf/test.pl 138699/1 (138700/1) transaction 4578643; promote; 2014/08/01 10:22:37 ; user: sam /./abc/desf/test.pl 138699/1 (138700/1) EOT print qq{[[$result]] \n\n}; while ($result =~ m{ transaction \s (\d+) .*? user: \s* (\S+) \s* (?| [#] \s* ([^\n]*) | ()) # comment or empty string to $3 }xmsg) { my ($transaction, $user, $comment) = ($1, $2, $3); printf "%-12s %-10s %s\n", $transaction, $user, $comment; }

      Output:

      c:\@Work\Perl\monks\sravs448>perl alt_branch_reset_1.pl [[transaction 1234567; promote; 2014/08/01 10:22:37 ; user: john # Performing a "promote" on file. /./abc/desf/test.pl 138699/1 (138700/1) transaction 4578643; promote; 2014/08/01 10:22:37 ; user: sam /./abc/desf/test.pl 138699/1 (138700/1) ]] 1234567 john Performing a "promote" on file. 4578643 sam

      Thanks ..it worked
Re: Reg the split expression
by Anonymous Monk on Aug 04, 2014 at 20:27 UTC
    try /transaction (\d+).*user:\s*(\S+)\s*(?:#\s*(.*))/g (untested), note that $3 will be undef and you may have to write $3//'' later to avoid warnings about that.
      typo! /transaction (\d+).*user:\s*(\S+)\s*(?:#\s*(.*))?/g (missing ? at the end)