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

File A

aa 22 310 1 300

ab 40 200 2 228

ac 51 230 10 241

df 23 240 22 242

ef 34 370 24 282

gh 24 125 22 210

df 76 290 11 232

wq 13 170 20 180

ss 89 288 12 270

Question is

I want to subtract 5th column with 4th column and That each one should be greater than 70 % of the top one of the third column (310) then print only thos e lines

Means say in the third column of top row is 310

then 70% of 310 is 70*370/100 is 217

Then subtract of 5th column with 4th column > 217 then only print which have greater than 217

aa 22 310 1 300

ab 40 200 2 228

ac 51 230 10 241

df 23 240 22 242

ef 34 370 24 282

df 76 290 11 232

ss 89 288 12 270

!/usr/bin/perl -w open (FILE,"$ARGV[0]") or die; my @temp =<FILE>; close FILE; foreach (@temp){ my @t= split("\t",$_); # $t[2],"\n"; if( $x =~ ($t[4]-$t[3]) > 70 * /100) { print "$x\n"; } }

Question si how to get top row of third column and then print these output

Replies are listed 'Best First'.
Re: to print l columnwise and subtraction
by kennethk (Abbot) on Jul 20, 2010 at 14:53 UTC
    Please test code before you post. Ignoring the missing hash on your hash-bang line (#!/usr/bin/perl -w), the code if( $x =~ ($t[4]-$t[3]) > 70 * /100) { does not compile. Specfically, I get the error:
    Search pattern not terminated at line 12.

    As well, wrap input and output in <code> tags as well as source. This is important in this case because you are splitting on tabs, but your post does not contain tab characters. Read How do I post a question effectively?.

    I am having a significant amount of difficulty understanding your spec. Assuming your first data are your input and the second are your output, the only filter I can find is that column 5 - column 4 > 217. The following code yields this result:

    #!/usr/bin/perl -w #open (FILE,"$ARGV[0]") or die; my @temp =<DATA>; #close FILE; foreach (@temp){ my @t= split("\t",$_); # $t[2],"\n"; if( ($t[4]-$t[3]) > 217 ) { print "$_"; } } __DATA__ aa 22 310 1 300 ab 40 200 2 228 ac 51 230 10 241 df 23 240 22 242 ef 34 370 24 282 gh 24 125 22 210 df 76 290 11 232 wq 13 170 20 180 ss 89 288 12 270

    Note this is fairly messy since I tried to use your posted code. I might have written something closer to

    #!/usr/bin/perl use strict; use warnings; #open (my $fh, '<', $ARGV[0]) or die "Open failed: $!"; while (<DATA>){ my @values = split /\t/; if( ($values[4]-$values[3]) > 217 ) { print; } } __DATA__ aa 22 310 1 300 ab 40 200 2 228 ac 51 230 10 241 df 23 240 22 242 ef 34 370 24 282 gh 24 125 22 210 df 76 290 11 232 wq 13 170 20 180 ss 89 288 12 270
Re: to print l columnwise and subtraction
by Utilitarian (Vicar) on Jul 20, 2010 at 14:56 UTC
    You already have the first row in $temp[0] So
    my @first=split(/ /,$temp[0]); my $minimum_printable=$first[2] * 0.7;
    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: to print l columnwise and subtraction
by jethro (Monsignor) on Jul 20, 2010 at 15:11 UTC

    If I understand you correctly, you want to compare the difference of each line with 70% of the value of the *first line* third column value. If that is the case, simply do this:

    my $minvalue; foreach (@temp){ my @t= split("\t",$_); $minvalue= $t[2] if (not defined $minvalue); if( $x =~ ($t[4]-$t[3]) > 70 * $minvalue/100) { ...

    This method works whether you have the whole file in memory or are reading it line by line from a file.

    Note that you can move the calculation of the 70% to the line where you initialize $minvalue, so that it is calculated once instead of every time through the loop.

Re: to print l columnwise and subtraction
by toolic (Bishop) on Jul 20, 2010 at 14:56 UTC
    Your output doesn't seem to match your description, but I think you want something like this:
    use strict; use warnings; while (<DATA>) { next unless /\S/; my @c = split; my $p = 0.7 * $c[2]; print if ($c[4] - $c[3] > $p) } __DATA__ aa 22 310 1 300 ab 40 200 2 228 ac 51 230 10 241 df 23 240 22 242 ef 34 370 24 282 gh 24 125 22 210 df 76 290 11 232 wq 13 170 20 180 ss 89 288 12 270

    prints out:

    aa 22 310 1 300 ab 40 200 2 228 ac 51 230 10 241 df 23 240 22 242 gh 24 125 22 210 df 76 290 11 232 wq 13 170 20 180 ss 89 288 12 270
Re: to print l columnwise and subtraction
by suhailck (Friar) on Jul 20, 2010 at 14:52 UTC
    An awk solution

    awk 'NR==1{max=0.70 * $3}(($5-$4) > max) { print }' filename


    ~suhail
      Thanx other question is like tht same kind

      aa 22 310 1 300

      ab 40 200 2 228

      ac 100 270 10 241

      df 23 240 22 180

      ef 100 270 24 282

      gh 24 125 22 190

      Output should be not the maximum present in the third column but if $2 cloumn is 100 then take third column is 270 then 70% of $5-$4 > $3 (when it is 100 in the 2nd column)

      awk '{if ($2=="100") print $3}' aaq | uniq -c |awk '{print $2}'

      How to write in one line

       awk '{if ($2=="100") max=0.70 * $3}(($5-$4) > max) { print }' $file Tht code written is right??