"Christmas - the time to fix the computers of your loved ones" « Lord Wyrm

Perl Regexp

M.I.P.S. 27.04.2005 - 14:11 517 0
Posts

M.I.P.S.

Big d00d
Avatar
Registered: Mar 2002
Location: On the inside
Posts: 324
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;
}
Kontakt | Unser Forum | Über overclockers.at | Impressum | Datenschutz