Home > Archive > PERL Beginners > October 2006 > iterate through and array?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
iterate through and array?
|
|
| Charles Farinella 2006-10-12, 6:59 pm |
| How can I iterate through an array and if I find a match do something
without doing that thing for every element in the array. I've been
trying to do it with foreach, but that isn't working.
--
------------------------------------------------------------------------
Charles Farinella
Appropriate Solutions, Inc. (www.AppropriateSolutions.com)
cfarinella@AppropriateSolutions.com
voice: 603.924.6079 fax: 603.924.8668
| |
| Eric Waguespack 2006-10-12, 6:59 pm |
| I am sure someone else can do it better, but how about the following?
foreach (@rray){
if (/match/) {print "yay";}
}
On 10/10/06, Charles Farinella <cfarinella@appropriatesolutions.com> wrote:
> How can I iterate through an array and if I find a match do something
> without doing that thing for every element in the array. I've been
> trying to do it with foreach, but that isn't working.
>
> --
> ------------------------------------------------------------------------
> Charles Farinella
> Appropriate Solutions, Inc. (www.AppropriateSolutions.com)
> cfarinella@AppropriateSolutions.com
> voice: 603.924.6079 fax: 603.924.8668
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>
| |
| Paul Lalli 2006-10-12, 6:59 pm |
| Charles Farinella wrote:
> How can I iterate through an array and if I find a match do something
> without doing that thing for every element in the array. I've been
> trying to do it with foreach, but that isn't working.
Your question is far too vague. Please be more precise, please provide
sample input and output. Please define "isn't working".
Iterate through the array. In each iteration, determine if the current
element is a "match" (for whatever definition of "match" you mean). If
it is, do your "something".
You also don't state whether you expect to find only one element that
"matches", and if so, then you should stop searching the array. If
that's true, add a "last;" statement right after your "something".
for my $elem (@array) {
if (found_match($elem)){
do_something();
last;
}
}
Without more information than you provided, the definitions of the
found_match and do_something subroutines are left to you.
One last thing -- are you sure you shouldn't be using a hash to begin
with? Iterating through an array looking for a specific value is one
of the prime indications that you should be using a hash. Of course,
with the information you (didn't) provide, it's not really possible to
know if the hash is the correct solution for you. It is if and only if
1) your data are unique (ie, no duplicates), and 2) a "match" is a
character-for-character exact equivalence. If that's the case, change
your code to get rid of the array and instead make a hash where the
keys are the current values of the array, and the values of the hash
are simply '1' each, and then:
if (exists $hash{$elem}){
do_something();
}
Paul Lalli
| |
| Charles Farinella 2006-10-12, 6:59 pm |
| Eric Waguespack wrote:
> I am sure someone else can do it better, but how about the following?
>
> foreach (@rray){
> if (/match/) {print "yay";}
> }
Thanks, this will work for printing as you suggest. What I'm trying to
do is replace this:
========================================
========================
my $checkThisMessage = $name . $company . $email . $telephone . $comment;
if( ! (
($email eq '') or
($checkThisMessage =~ m/abc123/i) or
($checkThisMessage =~ m/00000/) or
($checkThisMessage =~ m/ringtones/i) or
($checkThisMessage =~ m/dresses/i) or
($checkThisMessage =~ m/handbags/i) or
($checkThisMessage =~ m/phentermine/i)
) ) {
$msg->send
========================================
=========================
We have webforms for our customers and we're getting spammed. I'd like
to put all the phrases into an array instead of all of this 'or' business.
> On 10/10/06, Charles Farinella <cfarinella@appropriatesolutions.com> wrote:
>
--
------------------------------------------------------------------------
Charles Farinella
Appropriate Solutions, Inc. (www.AppropriateSolutions.com)
cfarinella@AppropriateSolutions.com
voice: 603.924.6079 fax: 603.924.8668
|
|
|
|
|