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

Gaal Yahas gaal at forum2.org
Thu Mar 5 07:52:29 PST 2009


Reply below relates to Perl 6.

On Thu, Mar 5, 2009 at 5:10 PM, Berler Chanan <bc.other at gmail.com> wrote:
>
> Since I got some background as DBA also, some operators would have make it
> easy for me working with Perl.
> Tell you what, I would have prefer a set of operators dedicated for arrays
> Like "in"  find the value in the array

Probably won't happen, because array searches are linear. But Perl 6
has a Set type with efficient searches.

>     "max" find the max value
>     "min" find the min value
>     "avg" find the avg value (assume numeric)

If you do avg, next people will ask for median, geometric and harmonic
mean... I don't think they belong as part of the language.

>     "first" find the first elemnt
>     "last" / "top" find the last element

What do you mean, find them? How is this different from @arr[0] and
@arr[-1]? ($arr[0] and $arr[-1] in Perl 5 syntax).

>     "find" find the location of the element in the array

In Perl 6, you can do

for @array.kv -> $index, $value {
  say "$index) $value";
}

>
> These are useful operators, which as I believe should be part of the Perl
> standard,
> Without using any external modules.
>
> As for if numeric or character, I would suggest adding a default: numeric,
> option for strings compare.

Perl 6 has those. (~ is stringy, + us numeric in prefixes to operators)

> PS: why not adding also:
>
> @array + elemnt -> adds the element
> @array - elemnt -> removes first element
> @array + @array -> return one array with both elements
> @array - @array -> return subtract of arrays (all elements which are found
> on both arrays)
> @array ~ @array -> xor of arrays (elements found on the first array and are
> not found on the second one).

None of these is really well-defined. "Adding" an element to an array
-- where? If you want "push" or "unshift", you know where to find
them.

> @array++        -> increase by 1 for all elements found (postfix)

@array >>++

> ++ at array        -> increase by 1 for all elements found (prefix)
> @array--        -> decrease by 1 for all elements found (postfix)

@array >>--

> -- at array        -> decrease by 1 for all elements found (prefix)
>
> The list can go on....but all these are not implemented as far as I know as
> operators
> Please correct me if I am missing anything, would help me a lot

Adding values of two arrays:

@a1 >>+<< @a2

> PS: using modules, well I can but why shouldn't these be standard ?

You will always be able to think of things you would have liked the
language to make available to you. The problem is, can it do that for
everybody?

> Thanks
> Chanan
>
>
> -----Original Message-----
> From: perl-bounces at perl.org.il [mailto:perl-bounces at perl.org.il] On Behalf
> Of Offer Kaye
> Sent: Thursday, March 05, 2009 4:48 PM
> To: Perl in Israel
> Subject: Re: [Israel.pm] Is there an operator like IN in perl
>
> 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) )
> }}}
>
> 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,
> --
> Offer Kaye
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl
>
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl
>



-- 
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/



More information about the Perl mailing list