PerlRookie has asked for the wisdom of the Perl Monks concerning the following question:

Right now I have if ($word =~ $w) { next; I want to make sure I am not duplicating words, but I don't want to leave out "bar", if bartender is a $word. So I trying to write the code that will evaluate each character. I know, such a rookie.

Replies are listed 'Best First'.
Re: Word Evaluation
by artist (Parson) on Jan 29, 2003 at 16:24 UTC
    Hi PerlRookie
    Welcome to the perlmonks.org site. Now that you have registered yourself, I like you to point out few resources on this site which would be helpful to you along the path to explore and gain valuable insight of experts here.

  • Tutorial
  • On asking for help

    Also looks like you are new to perl. Pick up a good book on perl such as "Learning Perl" to continue you study

    As far as your question goes.. please explain us what actually you want to do, and what do you mean by evaluate each character in which case.

    Artist

      Thank you for replying. I am very new to this, so sorry for the confusion. I am adding words to a line, but do not want to duplicate. The 2 strings are $words and $w. However, if advertise is one word, I don't want the word "ad" to be left out. My expression is only checking if the 2 words are equal to each other, I need to be checking past the first 2 characters.
        If I understood right, you have a list of words in a string and you want to add a word to that string, the new word should not exist already in the words string, am I right till here? If yes then you should do as follows:
        # suppose you have: $line = qw(advanture door tree); # and $word is: $word = 'ad'; ... if ($line =~ /\b$word\b/) { next; } else { # add $word to the list } # only if $word will be exactly 'adventure' or 'door' it will be skipe +d over
        '\b' is a word boundry like a space a comma (anything that is not a-z, A-Z, 0-9 or '_'), so the regexp in the 'if' statement is exactly what you need.

        Hotshot
Re: Word Evaluation
by hardburn (Abbot) on Jan 29, 2003 at 16:49 UTC

    Let me see if I understand your problem: you have a list of strings, such as "bar", "bartender", and "foobar". You want to get rid of any duplicates, but you have to make sure that "bar" won't match against "bartender" or "foobar".

    First solution that comes to mind is to ignore the regex and simply use string eq:

    next if($word eq $w);

    If you really need a regex for some reason, use \s to match whitespace around the word:

    next if($word =~ /\s$w\s/);
      Probably better check for word boundaries around the word, as they will also match the beginning and end of string;

      next if $word=~/\b$w\b/;
        Or you can use the slightly more longwinded form using alternations that allows $w to start with non-work characters (\b looks at the change between \W to \w not \s and \S):
        next if $word =~ /(^|\s)$w(\s|$)/;

        --
        integral, resident of freenode's #perl
        
Re: Word Evaluation
by hotshot (Prior) on Jan 29, 2003 at 16:09 UTC
    first, try reformating your questions next time.
    Secondly, I didn't quite undestand your question what's in $w. '=~' is used for regexp, and unless $w is a pattern, it's a mistake, please give a little more details.

    Hotshot
Re: Word Evaluation
by OM_Zen (Scribe) on Jan 29, 2003 at 16:48 UTC
    Hi ,

    if ($words =~ m/\s+bar\s+/){ next; }


    if that is what you are asking or mean a bar separated by spaces on both the start and after the word here as like bar