in reply to Re: Match key and return value in some order
in thread Match key and return value in some order

Hello Monk

I hope you can help me, yesterday I tried your code as it is and it was ok, but when I use it in with my program where I have my hash of array and my files with questions, the results I got were the following:

How/WRB1 hot/JJ1 is/VBZ1 the core/NP1 of/IN1 the earth +/NP2 ?/
ARRAY(0x8152bb0) or

the values of my hash /NP1/VBZ1(so)/JJ1 and /NP1/IN1/NP2/VBZ1(really)/JJ1 when I changed...
print "@$answer";
I know is a problem of dereference of the var, but I have tried the whole night but I am blocked!! , I even read the perlref.

Thanks for any help

Replies are listed 'Best First'.
Re: Re: Re: Match key and return value in some order
by BrowserUk (Patriarch) on Nov 04, 2002 at 19:36 UTC

    I would willingly try to help, but you will have to help me by making your question/problem a little clearer.

    The easiest way to do this would be to post (the relevant bits of) your code plus the inputs and failing output. If it is difficult to extract the relevant parts, and too big to post it all, then perhaps you could make the code visible on the net somewhere and post or /msg a url and I will try to take a look.


    Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy
      Here my program it is really short, so I will post it here

      #!/usr/bin/perl -sw use Data::Dumper; use strict; local $\=$/; my %patterns; open(PFILE, "whatpattern.txt")|| die "Cannot open whatpattern.txt file +"; while (<PFILE>) { chomp; next unless $_; my @tags = split /; |:/; my $key = shift @tags; $patterns{$key} = \@tags; } open(TESTFILE, "alltestfile")|| die "Cannot open alltestfile"; while(defined(my $question=<TESTFILE>)) { my @qkeys = $question =~ m!(/[A-Z0-9]+)!g; my @qvalues = $question =~ m!(.*?)(?:/[A-Z0-9]+\s+)!g; my %questionlookup; @questionlookup{@qkeys} = @qvalues; my $key = join'',@qkeys; print $question."\n\n"; if (my $answer = $patterns{$key}) { $answer =~ s!\s*$_\s*! $questionlookup{$_} !g for qkeys %questionloo +kup; $answer =~ s/\:/\n\n/g; print $answer; } else{ print "No Match\n"; } }

      As you see I did not change much your code. Do I have to pass the array of values as a reference? Thanks

        First problem is that there is a typo in the code you posted. When doing the substitution, you had for qkeys %questionlookup; instead of keys.

        However, I don't think that is the real problem as your code wouldn't compile without correcting this. So that leaves a second possibility - the format of your patterns file is not quite the same as I used in my answer.

        Please go back to that answer and note the comment

        #! Added leading and removed trailing /'s for consistancy

        I slightly modified the format of your patterns as you supplied them as this made the format more consistent which greatly simplified the regexes used for parsing them.

        As I don't have access to your patterns file, I re-used the (modified) versions that you supplied for the original question in the attached code. Using this, and with the correction above, your program works fine.

        I'm afraid that if your existing patterns file omits the /'s on the first tag and includes a trailing / on the end, then you wil either have to

        • Modify your patterns file or
        • modify the regexes (and possibly more) used in my example code in order to get this to work.

        I think that doing the former would be easier, as well as giving a more consistant format, but only you will know the scale of the problem and how easy that modification would be to do, so that decision is yours.

        Code and output


        Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy
        Input, will be the same as my first post, but instead of concatenate the answers, I print them out as a list