On Sep 12, ganesh said:
This is the warning messge I am getting when I executed my script.
Please find my analysis of the above warning
Input:
Line 1: 20(1): 125-126
Line 2: 20:125-126
Output:
Line 1: <volume>20</volume>(1): <fpage>125</fpage>-126
Line 2: <volume>20</volume>:<fpage>125</fpage>-126
My Code:
$line=~s!(\d+)(\(\d+\))?:(\d+)-!<volume>$1</volume>$2:<fpage>$3</fpage>-!g;
The warning message is being caused by your use of $2 in the right-hand
side of the s///. It's uninitialized in the case of Line 2, because a
capture group that is optional -- /(\(\d+)\))?/ in your regex -- stays
undef if it isn't matched. You can get around this by doing this instead:
s!(\d+)(\(\d+\)|):(\d+)-!...!g;
The '|' inside the 2nd capture group -- (\(\d+\)|) -- is an alternation,
and since there's nothing else after it, it means "or match the empty
string". This is an easy way of keeping the (NUMBER) part optional
without causing $2 to be undef if it isn't matched.
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://www.perlmonks.org/ % have long ago been overpaid?
http://princeton.pm.org/ % -- Meister Eckhart
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>