Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2008
    Posts
    7

    Unanswered: Perl - Pattern matching in sentences

    Hello perlmonks,

    I am trying to match the pattern derived in all the lines that are in a input file. But, I am bit confused with my code. The code could read the sentences in the file, but not reading the next line to match the pattern in the sentences. Please help me in solving the problem. Below is my perl code:

    #putting all phrases in to an array
    my @phrases = ($phrase1, $phrase2, $phrase3, $phrase4);
    foreach $new (@phrases) {
    print STDERR "the whole phrase is: $new\n";
    #defining a subroutine procedure by a variable name and calling the subroutine

    @array_return = procedure($new);
    print STDERR "the pattern is: @array_return\n";
    for($i=0; $i < @array_return; ++$i) {
    print STDERR "the value of for loop is: $array_return[$i]\n";
    }
    foreach $ref ($array_return[$i]) {
    #open a data file containing the sentences with the phrases to be matched
    $file = "sentences.txt";
    open(DAT, $file) || die "Couldn't open the file: $!\n";
    @lines=<DAT>;
    foreach $sentences (@lines) {
    print STDERR "the sentence is: $sentences\n";
    #match the words in the file
    if ($sentences =~ m/$ref/) {
    print STDERR "the word is matched in this sentence: $sentences\n";
    }elsif ($sentences =~ m/\b$ref\b/) {
    print STDERR "the word is matched at the word boundary in this sentence: $sentences\n";
    }
    }
    close DAT;
    }
    }

    The sample sentences in the input file are at the beginning of the code. Those sentences are in the file "sentences.txt". So, I would like to match the pattern derived at the end of subroutine in the file containing some sentences.

    I am sure, there is some confusion in the code either after storing the subroutine value in an array or incrementing part. I need each sentence with the pattern matched as the output. For eg:
    $sentences matched ---- pattern $ref

    Thanking in advance for your help and eagerly waiting.

  2. #2
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,800
    Provided Answers: 11
    Usually the <> operator returns a string, not an array. What are you expecting in the array @lines?

  3. #3
    Join Date
    Aug 2008
    Posts
    7
    hi,

    First of all, thanks for your reply.

    I wanted to read each line in the sentence and match the pattern derived. so, I stored all the lines in the file @lines and then for reading each line $sentences.

    when I tried to run the code I sent, actually the input file is opened but it is not reading the if statement for matching the pattern and I got some error message like: "uninitialised regexp at pattern" (i.e.,) $ref.

    Can you please explain me the actual problem in my code?

    Thanks.

  4. #4
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,800
    Provided Answers: 11
    The problem is that the <> operator reads one line. Check the contents of @lines after the line
    Code:
    @lines=<DAT>;
    I am not sure what you would find in there. Could be one element, could be several elements. It is hard to tell.

  5. #5
    Join Date
    Aug 2008
    Posts
    7
    I tried to print on the screen the elements of @lines and it shows all the lines in the input file.

    But how to match the pattern in each line?

  6. #6
    Join Date
    Aug 2008
    Posts
    7
    the line after @lines is the foreach loop. I could get the sentences in the input file printed on the screen. But I need each sentence and the matched pattern next to it (if loop) as the output.

    advice me on how to get this.

    thanks.

  7. #7
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,800
    Provided Answers: 11
    huh. Never knew that about the <> operator.

    Anyway, what is in @array_return? Or, easier to find, what is the value of $ref?

  8. #8
    Join Date
    Aug 2008
    Posts
    7
    @array_return returns the subroutine ("procedure") value where in my case it is the patterns (for eg: [A-z]oun [A-z]lock is the pattern & actual words in the file - for eg: noun/Noun Block/block) of the noun block I am trying to match in the actual text(sentences - input file).

    I defined the return value as $ref (which is there in the code without any meaning at the moment) that is the pattern (right hand side) and the sentences to be matched ($sentences - on left hand side). Actually after getting @array_return, I really dont know how to get the scalar value which I now imagined as $ref.

    Do you know how to derive the value of @array_return to scalar and search all sentences in the file matching that pattern?

  9. #9
    Join Date
    Aug 2008
    Posts
    7
    hi Mcrowley,

    I hope you will understand my previous reply. If not, i have posted the new code for the same problem but a complete one.

    Please advice me and write to me if you have any questions on understanding it.

    thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •