Adriano Allora wrote:
>
> hi to all,
>
> another silly question about a pattern matching which should work but it
> doesn't.
>
> I have a list af string similar to this one:
>
>     parola|n.c.,0,fem,sg,0|parola
>
> and I need to select all the chars before the pipe and put them in a
> variable.
>
> That substitution does'n work:
>
>     #!/usr/bin/perl -w
>     use strict;
>     my(%gen, %act, %record, $tex, $char, @parola, $zut);
>     while(<>)
>         {
>         $tex =~ s/^([^|]+).*/$1/o;
>         print STDOUT "$i errore sulla linea: $_\n" if !$tex;
>         }
>
> The error message is (for example):
>
>     Use of uninitialized value in substitution (s///) at
> ./contalettere.pl line 6, <> line 10.
>
> I suppose the rror is in the expression: [^|].
>
> Someone can help me?

Hi Adriano.

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.

If you want to do what you said, and put everything up to the pipe into a
variable (scalar $tex?) then

  /([^|]+)/;
  $tex = $1;

will do it for you. (It captures 'parola' in the example string. is that right?)

And, by the way, you haven't declared the $i in the print() call.

And, by the way again, there's no point in putting the /o modifier on the
substitute as the regex contains no variables and so will be compiled only once
anyway.

HTH,

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