27
~~ in perl id:astj

astj
• Category

## Sports

• view

205

1

### Transcript of Perlのsmart match演算子

~~ in perlid:astj

Ø|

m−oó\$aw

=ê@b”Ì•

m−v−ß¤o�x

�ž

\$a = 3; @b = (1,2,3,4);foreach \$elem (@a) { return 'Hit!' if \$elem == \$a;}

«ł›�¾Í–ƒq

grep { \$a == \$_ } @b;

perl 5.10 ı�“�

~~/æÄ

\$a ~~ @b@b”Ì•\$awm−�xÕþßç•þáÞ|—¡

use 5.010;\$a = 3; @b = (1,2,3,4);print "Hit!\n" if \$a ~~ @b;

Õþßç�¡„

\$a ~~ %cîáÓ™”ô»Éß³¢—¡

use 5.010;\$a = "Steve"; %c = ( "Steve" => "Jobs", "Bill" => "Gates" );print "Hit!\n" if \$a ~~ %c;

oo�¡„

m›��ô¹��Õ�Øq÷ö�¡

(Ø|v|¤œ5.14m¤ßvœvØ)

\$a = 3; \$b = (1,2,3,4);print "Hit!\n" if \$a ~~ \$b;

«¶Æ•5.14vœpush�vpop�vkeys�vØ

�ô¹��Õ��−œ|o�¡„

\$a ~~ \$d\$a == \$dºœ\$a eq \$dºœ

¶µvó^�¡„

@a ~~ @b\$a = [1,2,3];\$b = [1,2,3];print "Smart match!\n" if \$a ~~ \$b; # match!

ž�O

\$p = [ 1, 2, 3 ];\$q = [ 1, 2, 3, 4 ];print "Smart match!\n" if \$p ~~ \$q; # matchしない!

\$r = [ 1, 2, 3 ];\$s = [ 1, 2, [ 3, 4, 5 ] ];print "Smart match!\n" if \$r ~~ \$s; # matchするんだなぁ

µµ

perl 5.16”perldoc¢−�

Right operand is an ARRAY:

Left Right Description and pseudocode =============================================================== ARRAY1 ARRAY2 recurse on paired elements of ARRAY1 and ARRAY2[2] like: (ARRAY1[0] ~~ ARRAY2[0]) && (ARRAY1[1] ~~ ARRAY2[1]) && ... HASH ARRAY any ARRAY elements exist as HASH keys like: grep { exists HASH->{\$_} } ARRAY Regexp ARRAY any ARRAY elements pattern match Regexp like: grep { /Regexp/ } ARRAY undef ARRAY undef in ARRAY like: grep { !defined } ARRAY Any ARRAY smartmatch each ARRAY element[3] like: grep { Any ~~ \$_ } ARRAY

=êœÀ”~~“�oóÀ•~~|–−”�

\$a = [ 1, 2, 3 ];\$b = [ 1, 2, [ 3, 4, 5 ] ];

•u¦−(\$a ~~ \$b)�»fi—ß

( 1 ~~ 1 ) && ( 2 ~~ 2 ) && ( 3 ~ [3,4,5] ) # TRUE && TRUE && TRUE

ž�O∼Œ¶z¶›–x¤

îáÓ™»

Left Right Description and pseudocode =============================================================== HASH1 HASH2 all same keys in both HASHes like: keys HASH1 == grep { exists HASH2->{\$_} } keys HASH1 ARRAY HASH any ARRAY elements exist as HASH keys like: grep { exists HASH->{\$_} } ARRAY Regexp HASH any HASH keys pattern match Regexp like: grep { /Regexp/ } keys HASH undef HASH always false (undef can''t be a key) like: 0 == 1 Any HASH HASH key existence like: exists HASH->{Any}

\$a = { "Steve" => "Jobs", "Bill" => "Gates" };\$b = { "Steve" => "Ballmer", "Bill" => "Gates" };print "Smart match!\n" if \$a ~~ \$b; # matchする!!

∙µ∙µ�|z¶›–x¤

H•Øo¬o¬�|z–ºº|z–

—ÞO•Øº�}œ|o”� ( id:moznion œ )

The smartmatch family of featuresare now experimental

³°¶ozwooø„ •¶›–|—o—|¤

ï�

g