Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Using smartmatch with split function

by gurpreetsingh13 (Scribe)
on May 26, 2014 at 07:35 UTC ( [id://1087403]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks,
Here is again a simple one. But couldn't find any answer in any tags.
What I want is just to get out an array from split function and apply smartmatch operator over it.
My major purpose is to do the work with fewer and fewer variables within the code, that is why I am interested in this.
This is just an example to get clear picture.
Now this code works fine.
perl -e 'use v5.14;$a=3456; my @all=split (//,$a);print "Yes" if 3 ~~ +@all;'

But this fails.
perl -e 'use v5.14;$a=3456; print "Yes" if 3 ~~ split (//,$a);'
I want to eliminate that @all array there. It might be possible using 'grep' or 'any' or 'regex' or any of those List utility type modules. But is there any way I can simply impose a list context on split function.
The doc of smartmatch says that smartmatch operator looks at the RHS to see if that is an array and then uses grep.

Any ARRAY smartmatch each ARRAY element[ 3] like: grep { Any ~~ $_ } ARRAY


Replies are listed 'Best First'.
Re: Using smartmatch with split function
by Athanasius (Archbishop) on May 26, 2014 at 07:55 UTC

    Imposing list context on the split function won’t help, because the smartmatch operator infers the required behaviour only when it finds the righthand side to contain an array, not a list. So you can do this:

    17:49 >perl -we "use v5.14; $a = 3456; print 'Yes' if 3 ~~ @{[split(// +, $a)]};" Smartmatch is experimental at -e line 1. Yes 17:49 >

    which creates an anonymous array reference from the output of the split and then immediately dereferences it back to an array.

    Whether that is really an improvement over using an explicit array is another question...

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      The smartmatch operator takes an array reference as argument:
      % perl -MO=Deparse -e '1 ~~ @{[1,2]}' Smartmatch is experimental at -e line 1. 1 ~~ \@{[1, 2];}; -e syntax OK
      simplifying the code to: 3 ~~ [split //, $a];
      Thanks. Worked perfectly.
Re: Using smartmatch with split function
by boftx (Deacon) on May 26, 2014 at 16:35 UTC

    Given that smart match might be going away (or undergo extensive change) you might want to see this node.

    It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2024-04-16 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found