in reply to Re: Failed array attemp
in thread Failed array attemp

Looks like exactly what I want but does not work. I got rid of syntax error by substituting ~~ with == but the results is wrong.
@x = qw(1 4); @y = qw(1 2 3); push @z, (grep { $_ == \@y } @x) ? 'y' : 'n'; print $z[0]; print $z[1];

$z[0] gives me now and $z1 is uninitilised.

Replies are listed 'Best First'.
Re^3: Failed array attemp
by tobyink (Canon) on May 13, 2012 at 21:54 UTC

    The ~~ was intentional. It's the smart match operator. == won't cut it.

    Smart match was introduced in Perl 5.9.3, about 6 years ago.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      What if I have perl v5.8.5 (company provided and cannt upgrade)?

        There's this, but it's straying into seriously unreadable territory:

        push @z, (grep { my $x = $_; grep { $x == $_ } @y } @x) ? 'y' : 'n';
        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re^3: Failed array attemp
by stevieb (Canon) on May 13, 2012 at 21:57 UTC

    You have to be using perl v5.10 or higher to use the smartmatch operator (~~).

Re^3: Failed array attemp
by Anonymous Monk on May 13, 2012 at 22:01 UTC
    I keep getting syntax error and can't change the version so it must be old. Sorry to bother but anyway of making this work without smart catching? (can change slightly by I will be using this scheme a lot so I like the one line clean code. Thanks again.
    push @z, (grep { $_ ~~ \@y } @x) ? 'y' : 'n';

      If you are going to be "using this scheme a lot", I'd recommend you upgrade, or re-review this post I wrote which is similar to your original code in your question but it actually works.

      Admittedly, I didn't even think to use smartmatch, so kudos to tobyink for that.

      Without smartmatch, I believe you have to do it in two steps.

      I guess no other way. Can't really upgrade as I am working on a server and I am not an admin. Thanks steveib I will just use what you wrote :).