[Israel.pm] Is there an operator like IN in perl

Shlomi Fish shlomif at iglu.org.il
Thu Mar 5 07:18:35 PST 2009


Hi Offer!

On Thursday 05 March 2009 16:48:09 Offer Kaye wrote:
> On Wed, Mar 4, 2009 at 4:21 PM, Berler Chanan wrote:
> > 2) Using hash is great but I looked for more suite and elegant solution
>
> You could simply wrap the hash solution in an appropriately names sub,
> giving you the elegant solution for example:
>
> {{{
> if ( is_in_array(item => 'George' , aref => \@Beatles) )
> }}}
>

The problem with this code excerpt is that if you lookup several values in the 
same unmodified array, then you'll prepare a hash out of the array times and 
again instead of compiling it once and looking it up. This is unless you cache 
or memoise the array somehow.

An OO solution would be better:

{{{{{
my $beatles_lookup = ArrayLookup->new(\@Beatles);
if ($beatles_lookup->has("George"))
{
 ...
}
}}}}}

> You could even put "sub is_in_array" in a separate .pm file and "use"
> it so that the user need never be exposed to the "ugly" code which
> implements the pretty and elegant interface :)
>
> Cheers,

Regards,

	Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Funny Anti-Terrorism Story - http://xrl.us/bjn7t

<mauke>    I'm not interested in what you're doing; what are you trying to 
           achieve?
<PerlJam>  mauke: I'm trying to achieve world peace and this regex is
           the last thing standing in my way! ;)




More information about the Perl mailing list