Split string into separate words, and then test to see if each word is the one we're looking for. : Split « String « Perl






Split string into separate words, and then test to see if each word is the one we're looking for.

     

#!/usr/bin/perl
use warnings;
use strict;
my $found = 0;
$_ = "string... 'i'";
my $sought = "people";
foreach my $word (split) {
    if ($word eq $sought) {
        $found = 1;
        last;
    }
}
if ($found) {
    print "Hooray! Found the word 'people'\n";
}

   
    
    
    
    
  








Related examples in the same category

1.Split up
2.Split words
3.A simple word-count program.
4.Using split function to split a string
5.Split string with ','
6.Find a word after splitting a string
7.Keeping the separators by having them in parentheses.