On Apr 8, 9:06 pm, sono...@fannullone.us wrote:
>         Three hours later, I finally had a brainstorm, and came up with the 
> idea to count the number of occurrences of the words I'm looking for.  Here's 
> what I came up with:
>
> my $str = "PO Box 6545 / 3546 Termbo Street";
> my $address_count = 0;
> $address_count++ while ($str =~ /box|street|avenue|lane|apo/ig);
> print "$address_count\n";
>
>         It's crude, and I know I'll have to tweak it, but it works.  Is this 
> approach O.K., or is there a better way to do it?
>
> Marc

Hi Marc,

It is detecting but not testing if any particular 2 words are in a
text field and I think that's what you explained you were testing for.
Here is a way to do that, but I think you have to explicitly check for
the occurance of one and the other with 'if' statements.

There is a little technique of using a for loop (with a scalar string)
to use the implicit '$_' variable because it simplifies the code that
checks for the 2 words.

You can say: if (/box/i && /street/i) {
rather than the more complex:  if ($str =~ /box/i && $str =~ /street/
i)

Hope this helps

Chris

#!/usr/bin/perl
use strict;
use warnings;
use 5.012;

my $str = "PO Box 6545 / 3546 Termbo Street";
for ($str) {
        if (/box/i && /street/i) {
                print "Found box and street\n";
        }
        if (/apo/i && /avenue/i) {
                print "Another branch\n";
        }
}


--
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