in reply to Only joining elements of an array that are defined

I can't help you with that code, there are two joins and so many undefined variables that trying to understand what you intend is hopeless.

However the following snippet may help resolve your problem:

use strict; use warnings; my @array = (1, 2, undef, 4, 5, undef, undef, 8, 9); print join ' ', grep $_, @array;

Prints:

1 2 4 5 8 9

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Only joining elements of an array that are defined
by tirwhan (Abbot) on Feb 14, 2006 at 10:34 UTC

    Just a nit,

    grep $_, @array

    will not include false but defined values:

    use strict; use warnings; my @array = (1, 2, undef, 4, 5, 0, undef, 8, 9); print join (' ', grep $_, @array)."\n"; print join (' ', grep { defined } @array)."\n";
    Output
    1 2 4 5 8 9 1 2 4 5 0 8 9

    All dogma is stupid.

      Argh, I've not been bitten enough by that in real code. I have been bitten enough here by it that I should know better by now!

      Thanks for clarifying that for OP and the gentle reminder for me. :)


      DWIM is Perl's answer to Gödel
      grep defined, @array

        <shrug/>Whatever works for you. I find that always using the block form of grep and map is a lot clearer for the compiler as well as human reader, and can prevent mistakes for more complicated expressions, at the cost of one extra character. So unless you're golfing it's probably better to stick with that.


        All dogma is stupid.
        grep length,

        Is better, because we don't want extra spaces for empty lines

    A reply falls below the community's threshold of quality. You may see it by logging in.