Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Perl6: Smartmatch scalar to array

by molecules (Monk)
on Aug 16, 2010 at 17:13 UTC ( #855326=perlquestion: print w/replies, xml ) Need Help??
molecules has asked for the wisdom of the Perl Monks concerning the following question:

The code below gives me the output
["1", "2", "3", "4"] scalar '1' not in 1 2 3 4
when clearly it is in the array. Am I doing something wrong? Is it a future implementation issue?
use v6; my @array = <1 2 3 4>; @array.perl.say; my $scalar = '1'; if $scalar ~~ @array { say 'scalar in array'; } else { say "scalar '$scalar' not in @array[]"; }
I'm using Rakudo Star on Windows 7, 64-bit.


Replies are listed 'Best First'.
Re: Perl6: Smartmatch scalar to array
by Fox (Pilgrim) on Aug 16, 2010 at 17:41 UTC
     $scalar ~~ any(@array)
    does what you meant, don't know if it's supposed to be the default though.
      Indeed, it is precisely because Perl 6 has junctions that we do not need to assume junctional semantics on smartmatched lists; Perl 5 does not have junctions, so it does assume that. Instead, Perl 6 can use a list to smartmatch another list, which is also a useful behavior--especially since wildcards are supported. For instance, (1,2,3,4,5) ~~ (*,2,3,*) returns true.

        Thanks. I had assumed that smartmatch would be identical between Perl 5 and Perl 6, but I like the Perl 6 way better. It seems more maintainable.

        Also, thanks for all of your work on all of the Perls.

      Thanks. That also makes the intent clearer.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://855326]
Front-paged by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2016-10-24 00:54 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (302 votes). Check out past polls.