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

Consider this code:
#!/usr/bin/perl use warnings; use strict; my @array = qw( testme ); my $string = qq! first ! . ( grep /testme/, @array ? qq! yes ! : qq! no ! ) . qq! end !; print $string;

The output is first 0 end.
I would like to get this output: first yes no. Can someone help me? Thanks.

Replies are listed 'Best First'.
Re: Grep-Ternary Operator Question
by ikegami (Patriarch) on Oct 05, 2010 at 18:04 UTC

    Precedence issue, so use parens to disambiguate.

    grep /testme/, @array ? qq! yes ! : qq! no !

    means

    grep /testme/, ( @array ? qq! yes ! : qq! no ! )

    You want

    grep(/testme/, @array) ? qq! yes ! : qq! no !

    or

    (grep /testme/, @array) ? qq! yes ! : qq! no !

    Update: AnomalousMonk mentioned "Perhaps make clear that both solutions need to be enclosed in original set of parens to work."

    Replacing

    grep /testme/, @array ? qq! yes ! : qq! no !

    with

    (grep /testme/, @array) ? qq! yes ! : qq! no !

    means

    my $string = qq! first ! . ( grep /testme/, @array ? qq! yes ! : qq! no ! ) . qq! end !;

    becomes

    my $string = qq! first ! . ( (grep /testme/, @array) ? qq! yes ! : qq! no ! ) . qq! end !;

      This may be another case of missing the forest for the trees, but what is going on in the version in lines 27-30?

      #!/usr/bin/perl use warnings; use strict; # [id://863649] print "\n\t First test: incorporating correction by ikegami\n"; my @array = qw( testme foo bar ); my $i = 1; for my $string(@array) { my $test = $string; my $string2 = qq! first ! . (grep /$test/, $string) ? qq! yes ! : +qq! no ! . qq! end !; print "\$test: $test | \$string: $string | \$string2: $string2 | $ +i \n"; $i++; } print "\n\t now testing with 'blivitz'\n"; for my $string(@array) { my $test = "blivitz"; my $string3 = (grep /$test/, $string) ? qq! yes ! : qq! no !; print "\$test: $test | \$string3: $string3 | $i \n"; $i++; } print "\n\t BUT...\n"; my $string = qq! first ! . (grep /blivitz/, @array) ? qq! yes ! : qq! no ! . qq! end !; print $string . " -last test, lines 27-30 \n"; =head OUTPUT of 863649.pl as corrected by [ikegami] and extended First test: incorporating correction by ikegami $test: testme | $string: testme | $string2: yes | 1 $test: foo | $string: foo | $string2: yes | 2 $test: bar | $string: bar | $string2: yes | 3 now testing with 'blivitz' $test: blivitz | $string3: no | 4 $test: blivitz | $string3: no | 5 $test: blivitz | $string3: no | 6 BUT... yes -last test, lines 27-30 =cut

      WTF?!? grep is finding "blviitz" in @array? (or, ww is having another senior moment?)

        Note the lack of 'first' and 'end' in the output.

        my $string = qq! first ! . (grep /blivitz/, @array) ? qq! yes ! : qq! no ! . qq! end !;

        is

        my $string = ( qq! first ! . (grep /blivitz/, @array) ) ? ( qq! yes ! ) : ( qq! no ! . qq! end ! );

        You removed a set of parens. See the update I made at the same time you were posting.