How do pass one array and one string as arguments to a function?

前端 未结 5 1285
感情败类
感情败类 2021-01-13 14:44

Because I can\'t find a convenient way to check if $str is in @array, I\'m trying to make one myself, but it is not working.

I guess it is

5条回答
  •  走了就别回头了
    2021-01-13 15:14

    How about $str ~~ @arr in a smartmatch? That's available in Perl 5.10.

    use 5.010;
    use strict;
    use warnings;
    
    my $str = 'three';
    my @arr = qw(zero one two three four);
    my @badarr = qw(zero one two four eight);
    
    say '$str ', $str ~~ @arr? 'is' : 'is not', ' in $arr.';
    say '$str ', $str ~~ @badarr? 'is' : 'is not', ' in $badarr.';
    

    Output, as expected:

    $str is in $arr.
    $str is not in $badarr.
    

提交回复
热议问题