develooper Front page | perl.perl5.porters | Postings from January 2011

Re: Using smart matching to find whether an array contains a string

Thread Previous | Thread Next
From:
Ed Avis
Date:
January 26, 2011 04:10
Subject:
Re: Using smart matching to find whether an array contains a string
Message ID:
loom.20110126T130201-325@post.gmane.org
Brad Gilbert <b2gills <at> gmail.com> writes:

>>Is it possible to find whether an array contains a given string?

>Actually, all you would have to do is make sure that the elements of
>the array are strings.
>
>    perl -E '@a = ("1"); say "1 x" ~~ @a'

Thanks.  In my original code the array is of strings so I could use this.
But when I dropped into the command line to test behaviour with some one-liners,
I reflexively picked (1, 2, 3) as my example array.

This is a slightly unperlish distinction; usually in Perl it doesn't matter
whether a scalar holds the number 42 or the string '42' - you can use either
of them in arithmetic or string operations with the same semantics and no
warnings.  This is a contrast to languages like Python where you have a visible
difference between numeric and string objects.  Smart matching might be the first
everyday case where the difference between numbers and strings becomes obvious
to the ordinary Perl programmer.

-- 
Ed Avis <eda@waniasset.com>


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About