John W. Krahn wrote:
> Rob Dixon wrote:
>
>>Adriano Allora wrote:
>>
>>>        $tex =~ s/^([^|]+).*/$1/o;
>>
>>Your regex is correct, but it doesn't do what you said you wanted! You're
>>substituting the entire string in $tex for just those characters up to
>>the first
>>pipe, but there's nothing in $tex - the data has been read into $_.
>>
>>If you want to do what you've written, then s/|.*// is a lot easier: it
>>just removes everything starting at the first pipe.
>
>
> The | is for alternation so if you want to match a literal | character you
> have to escape it:
>
> s/\|.*//s

Quite right John, Thank you.

>>If you want to do what you said, and put everything up to the pipe into a
>>variable (scalar $tex?) then
>>
>>  /([^|]+)/;
>>  $tex = $1;
>
>
> You should only use the numeric variables if the match was successful
> otherwise their contents may not be what you expected.
>
>    if ( /([^|]+)/ ) {
>        $tex = $1;
>        }

Mumia made the same point. In context, this was the first and only pattern match
in the program, so $1 would have remained undefined if the match had failed. But
I agree, the code was not correct in general and I should have made that clear.

I must stop posting after 1:00 am :-/

Rob


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to