Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Short version of database push for multiple variables

by Anonymous Monk
on May 13, 2012 at 17:39 UTC ( #970306=note: print w/ replies, xml ) Need Help??


in reply to Re: Short version of database push for multiple variables
in thread Short version of database push for multiple variables

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?


Comment on Re^2: Short version of database push for multiple variables
Re^3: Short version of database push for multiple variables
by BrowserUk (Pope) on May 13, 2012 at 18:00 UTC
    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?

Re^3: Short version of database push for multiple variables
by Eliya (Vicar) on May 13, 2012 at 18:05 UTC

    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!
        ... near "// 'NA'"

        There is no "//" in my code, so it's unlikely you'd get this error :)

Re^3: Short version of database push for multiple variables
by Eliya (Vicar) on May 13, 2012 at 20:19 UTC

    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.

        Then you just need to add parentheses:

        my @fields = map length() ? $_ : 'NA', split /\t/, $Line, -1;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://970306]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (11)
As of 2014-09-03 08:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (35 votes), past polls