Adriano Allora am Sunday, 24. September 2006 01:12:
> 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;
$tex is never set before used.
> print STDOUT "$i errore sulla linea: $_\n" if !$tex;
$i is not defined.
> }
>
> 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: [^|].
No, it's referring to the fact that $1 is never defined because $tex is never
defined :-)
Here's a first modified version:
#!/usr/bin/perl
use strict;
use warnings;
while(<DATA>) {
chomp;
s/^([^|]+).*/$1/;
print $_
? "ok: $_\n"
: "errore sulla linea $. ($_)\n";
}
__DATA__
heyq24614q35gh|-------------
efbäpkmmeth|qrhqeerherth
But you don't need a substitution, a simple match suffices:
#!/usr/bin/perl
use strict;
use warnings;
while(<DATA>) {
chomp;
my $tex= /(.*?)\|/
? do {warn "ok: $1\n"; $1}
: do {warn "errore sulla linea $. ($_)\n"; undef};
}
__DATA__
heyq24614q35gh|-------------
efbäpkmmeth|qrhqeerherth
Hope this helps!
Dani
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>