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:
Brad Baxter
Date:
January 26, 2011 06:18
Subject:
Re: Using smart matching to find whether an array contains a string
Message ID:
AANLkTi=a9oK0FEsfStaMqaX7TgPx3gZszKYepjuBDm8p@mail.gmail.com
On Wed, Jan 26, 2011 at 7:05 AM, Ed Avis <eda@waniasset.com> wrote:
> 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.

Perhaps I misunderstand the example, but there is no time that I'm aware
of when perl would store "1 x" in a scalar as a number.

-- 
Brad

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