URL: https://www.overclockers.at/coding-stuff/perl_regexp_141441/page_1 - zur Vollversion wechseln!
Hab in einem Perlscript zwei Regexp-vergleiche die beide in einer Schleife ablaufen. Der zuerst ausgeführte funktioniert, beim zweiten (in der sub) bekomme ich nur beim ersten Schleifendurchlauf ein Ergebnis, bei allen weiteren Durchläufen sind $1 und $2 undef obwohl er gemäß der Variablenihalte was finden müsste.
Woran happerts da?
Code:#!/usr/bin/perl -w %POS = ("NN" => "noun", "NNP" => "noun", "NNPS" => "noun", "NNS" => "noun", "JJ" => "adjective", "JJR" => "adjective", "JJS" => "adjective", "VV" => "verb", "VVD" => "verb", "VVG" => "verb", "VVN" => "verb", "VVP" => "verb", "VVZ" => "verb"); # searching ldoce for the word index regarding POS # Args: word, POS-tag # Return: ldoce word index sub ldoce_lookup { $lookup_word = $_[0]; $lookup_part_of_speech = $_[1]; my @ldoce_return = `ldoce search $word`; foreach $ldoce_return_string (@ldoce_return) { if ($ldoce_return_string =~ /^(??{$lookup_word})(\/\d+)?\:(??{$lookup_part_of_speech})\:(\d+)/) { return $2; } } return NA; } # processing arguments and generating arrays of the input while (<>) { if ($_ =~ /^(\S+)\t+(\S+)\t+(\S+)/) { push @word, $1; push @pos, $2; push @rest, $3; } } # lookup and output while (defined($word[0])) { $word = shift @word; $pos = shift @pos; $rest = shift @rest; if (defined ($POS{$pos})) { $part_of_speech = $POS{$pos}; $ldoce_index = ldoce_lookup($word, $part_of_speech); $ldoce_string = "\[ldoce $ldoce_index\]"; print "$word\t$pos\t$rest\t$ldoce_string\n"; } else { print "$word\t$pos\t$rest\t\n"; } $part_of_speech = undef; }
overclockers.at v4.thecommunity
© all rights reserved by overclockers.at 2000-2025