Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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.

Thanks!

Comment on Perl6: Smartmatch scalar to array
Select or Download Code
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.
      Thanks. That also makes the intent clearer.
      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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2014-09-30 12:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (368 votes), past polls