in reply to Match key and return value in some order
I tweaked your answer format a little to simplify things. See comment below.
#! perl -sw use strict; local $\=$/; #! Added leading and removed trailing /'s for consistancy my %answers = ( '/WRB1/JJ1/VBZ1/NP1/IN1/NP2' => '/NP1/VBZ1(so)/JJ1 : /NP1/IN1/N +P2/VBZ1(really)/JJ1', '/WP/NP1/VB1/VBN1/PP1' => '/PP1/PP2/NP1(are)/VB1', ); my $question = 'How/WRB1 hot/JJ1 is/VBZ1 the core/NP1 of/IN1 the earth +/NP2 ?/'; my @keys = $question =~ m!(/[A-Z0-9]+)!g; # Parse markers # Update: Fixed problem not capturing all words between tags #my @values = $question =~ m!(\w+)(?=/)!g; # Parse values my @values = $question =~ m!(.*?)(?:/[A-Z0-9]+\s+)!g; my %lookup; @lookup{@keys} = @values; # Build lookup my $key = join'',@keys; my $answer = $answers{$key}; $answer =~ s!\s*$_\s*! $lookup{$_} !g for keys %lookup; $answer =~ s!\s+:\s+! and the !g; #$answer = 'The' . $answer; # Update: No longer needed. print $answer;
Output: (revised after changes)
C:\test>210085 the core is (so) hot and the core of the earth is (really) hot C:\test>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Match key and return value in some order
by Anonymous Monk on Nov 04, 2002 at 00:57 UTC | |
by BrowserUk (Patriarch) on Nov 04, 2002 at 01:31 UTC | |
|
Re: Re: Match key and return value in some order
by Anonymous Monk on Nov 04, 2002 at 18:57 UTC | |
by BrowserUk (Patriarch) on Nov 04, 2002 at 19:36 UTC | |
by Anonymous Monk on Nov 04, 2002 at 20:12 UTC | |
by BrowserUk (Patriarch) on Nov 04, 2002 at 21:34 UTC | |
by Anonymous Monk on Nov 04, 2002 at 22:26 UTC | |
| |
by Anonymous Monk on Nov 04, 2002 at 20:34 UTC |