in reply to Short version of database push for multiple variables

Kinda depends what mean by "blank"?

If the file contains two successive tabs with nothing in between, the result of split will undef, and you can use defined-or:

my @a = map $_//'NA', split /\t/, $line;

If there might be spaces, or empty quotes between the tabs, you need to use else.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

Replies are listed 'Best First'.
Re^2: Short version of database push for multiple variables
by Anonymous Monk on May 13, 2012 at 16:39 UTC

    This seems perfect. I can just replace anything blank in a line with 'NA' and then append elements of (in your example) @a to appropriate lists. Thanks for that.

Re^2: Short version of database push for multiple variables
by Anonymous Monk on May 13, 2012 at 17:39 UTC
    A problem though. I presume it was supposed to be //'NA'/. The blanks are two succesive tabs but I get a illegal division by zero with this command. Any idea why?

      In case the original version doesn't work for you (error "Search pattern not terminated at ..."), your Perl might be too old (<5.10).  In this case, you could say instead

      my @a = map defined $_ ? $_ : 'NA', split /\t/, $line;

      Or upgrade Perl.

        My version is 5.8.5. When I try your code I get the following error; Did not find leading dereferencer, detected at offset 14434String found where operator expected at Process.pl line 146, near "// 'NA'" Thanks for your help!
      I presume it was supposed to be //'NA'/.

      No. What I posted was tested, working code. The defined-or operator is: //. Ie. The full line with a couple of extra spaces is:

      my @a = map $_ // 'NA', split /\t/, $line;

      The extra / you are adding is the cause of your divide by zero message.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

      BTW, AFAICT, split /\t/ on two consecutive tabs wouldn't return undef, but rather '' (the empty string):

      $ perl -MData::Dumper -e'@a = split /\t/, "foo\t\tbar"; print Dumper \ +@a' $VAR1 = [ 'foo', '', 'bar' ];

      So the whole "//" thing is moot anyway.  It wouldn't even work:

      $ perl -MData::Dumper -e'@a = map $_//"NA", split /\t/, "foo\t\tbar"; +print Dumper \@a' $VAR1 = [ 'foo', '', 'bar' ];

      In other words, you probably want:

      $ perl -MData::Dumper -e'@a = map $_ ne "" ? $_ : "NA", split /\t/, "f +oo\t\tbar"; print Dumper \@a' $VAR1 = [ 'foo', 'NA', 'bar' ];
        Thanks Eliya that worked great. However as jwkrahn wrote the split need -1 so it won't miss the last element. Jwkrahn your code gives me "Warning: Use of "length" without parentheses is ambiguous at" which is a shame cause it's a nice and easy to understand code.