Hello monks, the program I'm working on attempts to accomplish two things: 1: break a sentence (user inputed) into its elements (words or whatever seperated by a space character). 2: test those elements against a file of nouns to see if they match. if the first element in the sentence is a match, the program works the way I want it to (ex: Cars bla bla.) Cars will match, however if the element is not the first one (ex: I like Cars.), Cars will not match with Cars in the txt file. any help or tips/hints would be much appreciated thank you!

#!/usr/bin/perl my $sen = <STDIN>; chomp $sen; if($sen =~ s/(\.|\?|\!)$/ /g){ #get punctuation and replace with white +space $punctuation = $&; } while($sen =~ m/ /g){ # test for spaces in sentence my $pos = pos $sen; my $element = substr($sen,0,$pos,""); # get chunk of sentence chop $element; #remove end whitespace push(@senElements,$element); #push chunk into array } open(NOUNS,'<',"nouns.txt") or die "Can't open noun database: $!\n"; # # # attempt to recognize sentence elements as a noun via file nouns. +txt # # # foreach $element (@senElements){ while(<NOUNS>){ chomp(my $line = $_); $line =~ s/ |\n//g; #remove any space chars and newlines from file + line if($element =~ m/^($line)$/i){ print "\n!MATCH! ~ $element is a noun\n"; } } } close(NOUNS);

In reply to testing parts of a string against a word database by Rudolf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.