Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
But it isn't clear to me how to generalize this approach for more complicated expressions, such as "are all elements of my array equal to scalar 'foo'" and "is any element of my array equal to scalar 'bar'". I suppose I could say something like# One argument is true sub any { $_ && return 1 for @_; 0 } # All arguments are true sub all { $_ || return 0 for @_; 1 }
But is that really the best I can do? I might like to have something like# One argument is true sub any { $_ == 'bar' && return 1 for @_; 0 } # All arguments are true sub all { $_ == 'foo' || return 0 for @_; 1 }
Or is that asking for too much?if (all (@array == 'foo')) {
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: array comparison
by ikegami (Patriarch) on Dec 26, 2008 at 21:00 UTC | |
|
Re: array comparison
by FunkyMonk (Bishop) on Dec 27, 2008 at 00:21 UTC | |
|
Re: array comparison
by jdporter (Paladin) on Dec 27, 2008 at 02:04 UTC |