Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

wrong in minus

by yueli711 (Sexton)
on May 10, 2018 at 16:33 UTC ( [id://1214337]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I just want to the number of the first column to minus the second column. The results are all 0. Thanks in advance!

open(IN1,"tmp01") || die "Cannot open this file"; @lines1 = <IN1>; open(OUT,">tmp02") || die "Cannot open this file"; for $item1(@lines1){ chomp $item1; @tmp1=split(/t+\,$item1/); $a=$tmp1[1]-$tmp1[0]; print OUT $a,"\n"; } close(IN1); close(OUT);

Replies are listed 'Best First'.
Re: wrong in minus
by AnomalousMonk (Archbishop) on May 10, 2018 at 18:08 UTC
    ... I just want to the number of the first column to minus the second column.

    Note that the OPed statement
        $a=$tmp1[1]-$tmp1[0];
    yields the result of the second column minus the first column. (Actually, it gives you the result of the second array element minus the first and we're assuming these elements are input data columns, but that's another question.)

    Update: It just occurred to me that the OPed question might be properly paraphrased as "I want the number of the first column to be subtracted from the second column"; in that case, disregard the reply above.


    Give a man a fish:  <%-{-{-{-<

Re: wrong in minus
by Anonymous Monk on May 10, 2018 at 16:54 UTC

    I don't know what your data looks like (you should give us a sample), but making an assumption that you used split incorrectly, try this:

    @tmp1=split /\t+/, $item1;

      And the use strict; use warnings; advice would have given you an indication:

      $ perl -wE'my $item1="x\t\ty";my@tmp1=split(/t+\,$item1/);' Use of uninitialized value $_ in split at -e line 1. $ perl -wE'my $item1="x\t\ty";my@tmp=split(/t+/,$item1);say for@tmp' x y $ perl -wE'my $item1="x\t\ty";my@tmp=split(/\t+/,$item1);say for@tmp' x y

      Enjoy, Have FUN! H.Merijn

      Thank you so much! Yes! You are correct!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1214337]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-29 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found