Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

bug? why does this ~~ smart match to an array slice fail?

by flies (Novice)
on Sep 30, 2010 at 18:50 UTC ( [id://862830]=perlquestion: print w/replies, xml ) Need Help??

flies has asked for the wisdom of the Perl Monks concerning the following question:

I'm to see whether an element is in an array using ~~. Apparently, perl is treating an array slice as a scalar (size of slice):
my @ar = (0..10); my @ar2 = @ar[1..4]; for my $i (@ar) { # matches for $i==4 only print "$i ~~ slice? ", isTrue($i ~~ @ar[1..4]) # matches properly print "\t$i ~~ ar2? ", isTrue($i ~~ @ar2), "\n"; } sub isTrue { return ($_[0])?"true":"false"; }
I can use grep for this, but still. v5.10.0 built for x86_64-linux-thread-multi

Replies are listed 'Best First'.
Re: bug? why does this ~~ smart match to an array slice fail?
by ikegami (Patriarch) on Sep 30, 2010 at 19:02 UTC
    No special behaviour is documented for array slices. An array slice in list context returns its last element, so you're matching against that. Array slices should probably behave like array refs, but it's not documented to do so.

    Of course, you can do that yourself:

    $i ~~ [ @ar[1..4] ]
      "An array slice in list context returns its last element"
      didn't know that. explains this behavior. not a bug.
      I guess we are talking about array slices in scalar context. Only in scalar context the array slices return last element. And this is the biggest difference between array slices and arrays. The arrays return the number of elements. I guess in this situation, there's no bug. The ~~ operator works with arrays, not with lists, and array slice is the list. Unfortunately perl documentation does not cover the case of using list as the argument for smart match. But looking at the functionality it is clear, that operator works in scalar context with the objects that are passed to it. As result, the list of scalars always returns the last scalar.

        Unfortunately perl documentation does not cover the case of using list as the argument for smart match

        The smart match operator's operands are evaluated in scalar context. They can't possibly return a list. Furthermore, perlop and perlfunc are very clear as to what each operator returns in scalar context. There are a couple of exceptions:

        @array is special-cased to mean \@array
        %hash is special-cased to mean \%hash
        /pat/ is special-cased to mean qr/pat/

Re: bug? why does this ~~ smart match to an array slice fail?
by youlose (Scribe) on Oct 01, 2010 at 13:03 UTC
    Hello,

    let's look a little close to this link

    There is no variant Any ~~ Array, but we see It is always commutative, i.e. $a ~~ $b behaves the same as $b ~~ $a..

    Array Any array contains string grep $_ eq $b, @$a

    It's that we need. @$a - means that Array is a reference to array => you must use $i ~~ [ @ar[1..4] ]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://862830]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-20 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found