Thank you for the feedback.  I do apologize for not posting a working
example; I can't post the full code and I was attempting to extract the
offending sections.

I have no particular fondness for grep.  A search of postings on perlmonks
revealed a variation of the code I employed.  I am learning perl as quickly
and thoroughly as I can, but I am cannibalizing some code for the purpose of
completing my tasks within a foreseeable time frame.

Can you suggest a reference on hashes that will provide some clue as to how
they can be used for the problem I posted?  I've looked over Programming
Perl (3rd) and it's not entirely clear to me how to proceed with a hash.

Owen

On Mon, Apr 12, 2010 at 10:11 PM, Peter Scott <pe...@psdt.com> wrote:

> On Mon, 12 Apr 2010 21:06:58 -0500, Owen Chavez wrote:
> > 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);
> >
> > ...
> >
> > while (my $line = <>)
> > {
> >   chomp $line;
> >   my @strings = split /\s+/, $line;
> >   my @words = grep /\w+/, @strings;
> >   my $n_words += scalar(@words);
> >   $fst_prsn += scalar (grep {my $comp1 = $_; grep {$_ =~ /\b$comp1\b/ig}
> > @words} @prnouns1);
> > }
> > print "Result: Number of words: $n_words - First: $fst_prsn\n";
> >
> > The result produced by this code is incorrect:
>
> The result is correct, it's the code that is incorrect :-)
>
> > Result: Number of words: 10 - First: 6
> >
> > It's not counting the second "I" although I've included the /g modifier.
> >  Can anyone tell me why?  How can I accomplish this?
>
> First, it helps if you post a complete working example.  Yours isn't;
> $n_words isn't in scope when you print it.
>
> Second, you seem to be very fond of the grep function, but I don't think
> you understand what it is for or how it works.  It is not identical to
> the Unix utility.  Your problem is that when the result of the outer
> block is a list containing more than one element, you seem to think that
> should somehow let through more than one element from the list being
> passed in.  You will not get more elements out of grep than you put in.
>
> Third, you would achieve the goals of this code much more understandably
> if you used a hash.  If you don't know what hashes are, now is the time
> to learn.
>
> --
> Peter Scott
> http://www.perlmedic.com/     http://www.perldebugged.com/
> http://www.informit.com/store/product.aspx?isbn=0137001274
> http://www.oreillyschool.com/courses/perl1/
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>

Reply via email to