Owen Chavez wrote:
Hello!
I have a pattern matching question using Perl 5.10, Windows 7. Suppose I
have a file containing the following block of text:
Hello there TODD
I my We Us ourselves OUr I.
The file has 10 words, including 7 first-person pronouns (and 3 non-pronouns
that I have no interest in).
I've scrabbled together the following code:
#!/usr/bin/perl
use strict;
use warnings;
my @prnouns1 = qw(I we me us myself ourselves mine ours my our);
...
my $n_words = 0;
my $fst_prsn = 0;
while (my $line = <>)
{
# replace starting here
chomp $line;
my @strings = split /\s+/, $line;
my @words = grep /\w+/, @strings;
# to here with:
my @words = split /\W+/, $line;
my $n_words += scalar(@words);
$n_words += scalar( @words );
$fst_prsn += scalar (grep {my $comp1 = $_; grep {$_ =~ /\b$comp1\b/ig}
@words} @prnouns1);
for my $pronoun ( @prnouns ){
for my $word ( @words ){
$fst_prsn ++ if lc( $word ) eq lc( $pronoun );
}
}
}
print "Result: Number of words: $n_words - First: $fst_prsn\n";
--
Just my 0.00000002 million dollars worth,
Shawn
Programming is as much about organization and communication
as it is about coding.
I like Perl; it's the only language where you can bless your
thingy.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/