Harold Castro wrote:
> 
> #This line is too tricky for me, all I know is that it
> has a ternary hook operator which returns $_ if the
> expression my @fields = map length() is true,
> otherwise, substitute empty fields with 'NA'. I looked
> into perldoc -f map as well as length() and as far as
> I can only understand, an empty argument pass to
> length will make use of $_. While the 'map' as used in
> example on perldoc would translate a list of numbers
> to their corresponding character using this line:
> 
>  @chars = map(chr, @nums);
> 
> 
> Would you mind explaining me how did you tell it to
> substitute empty cells with 'NA' using the line below?
> I just can't seem to find any sort of like this
> substitution using, 's/ /NA/g'
> 
> my @fields = map length() ? $_ : 'NA', split /:/, $_,
> -1;

Assuming you start with the contents of $_ being:

"four:five:\n"

chomp() on the first line removes the newline so you are left with:

"four:five:"

split( /:/, $_, -1 ) splits the contents of $_ using the colon (:) as the
separator and returns the list:

( 'four', 'five', '' )

* note that without the negative number as the third argument it would only
return the list ( 'four', 'five' ), the negative number ensures that *all*
fields are returned.

map() processes each member of the list separately through a local copy of the
$_ variable and passes the result through to the assignment.  Inside the map()
the expression:

length() ? $_ : 'NA'

tests the string length of $_ and if it is zero it returns the string 'NA' or
else it returns the contents of $_.


HTH


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to