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

hello gurus,

Once again, I ask for help on this board.
I wrote below code to split on '|' and put default if it's empty line.. but I am not getting the results I want
What am I doing wrong?
Thank you so much in advance
use warnings; use strict; use Data::Dumper; my $file = '1|0||34|0|123'; print "file is $file\n"; my @m_file = map { ($_) ? $_ : 'default' } (split /\|/,$file); # try +w/ defined($_) as well print Dumper(@m_file);
Output I want 1 0 default 34 0 123

Replies are listed 'Best First'.
Re: confused w/ map and trueness
by planetscape (Chancellor) on Jun 16, 2010 at 17:39 UTC
Re: confused w/ map and trueness
by SuicideJunkie (Vicar) on Jun 16, 2010 at 16:23 UTC

    Zero is a false value, naturally.

    I suspect you want to test instead for a string length greater than zero.

Re: confused w/ map and trueness
by almut (Canon) on Jun 16, 2010 at 16:23 UTC

    Test if it's the empty string:

    $_ ne '' ? $_ : 'default'
Re: confused w/ map and trueness
by Anonymous Monk on Jun 16, 2010 at 16:29 UTC
    #!/usr/bin/perl -- use strict; use warnings; use Data::Dumper; my $file = '1|0||34|0|123'; print "file is $file\n"; my @m_file = map { length $_ ? $_ : 'default' } split /\|/, $file; print Dumper(\@m_file); __END__ file is 1|0||34|0|123 $VAR1 = [ '1', '0', 'default', '34', '0', '123' ];
Re: confused w/ map and trueness
by mikeraz (Friar) on Jun 16, 2010 at 16:53 UTC

    0 (zero) evaluates to false.

    $_ eq '' ? default : $_ or
    $_ eq "" ? default : $_
    seems to be what you want


    Be Appropriate && Follow Your Curiosity