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

suppose u have a file 1 0.4 2 1 0.2 -5 1 0.1 -18 1 0.11 7 0 0.13 10 0 0.22 11
From the second column and third column , u want to show at differen +t intervals keeping second column fixed and change the interval in +the third column. Like at less than 0.01 from the scond column and <9 and <8 how many nu +mbers are there in one output then again keeping 0.01 same as above and then <8 and >7 how many numb +ers ...respectively Then change the interval from less than 0.01 to >0.01 and less tahn 0 +.02 in the second column and again start from the third column with +<9 and >8 respectively
I have written the code
#!/usr/bin/perl -w open (FILE,"$ARGV[0]") or die; open (WRITE,">ca_0.01_1.txt"); @list =<FILE>; close (FILE); my $f =join ("",@list); my @t = split ("\n",$f); foreach (@t) { my @x = split("\t",$_); if(($x[1]<=0.01) && ($x[0]=="0")) { next if ( $x[2]>=-8); next if ($x[2]<-9); #next if ($x[0]!=0); #next if ($x[0]!=1); print WRITE @x,"\n"; } } close (WRITE); exit;
  • Comment on Keeping one colmun fixed and second column changed at different intervals to print the output
  • Select or Download Code

Replies are listed 'Best First'.
Re: Keeping one colmun fixed and second column changed at different intervals to print the output
by halfcountplus (Hermit) on Nov 30, 2010 at 13:18 UTC

    Your question makes no sense to me, but here's one observation:

    open (FILE,"$ARGV[0]") or die; @list =<FILE>; close (FILE); my $f =join ("",@list); my @t = split ("\n",$f);

    What is the point of doing that? @t will be exactly the same as @list except the newlines are removed, which they are more easily, efficiently, and clearly removed with chomp().

    Also: indent your code. You may think this is an irrelevant or spurious criticism now, but unless you give up programming, you will change your mind very soon.

Re: Keeping one colmun fixed and second column changed at different intervals to print the output
by cdarke (Prior) on Nov 30, 2010 at 13:45 UTC
    Something like this?
    use warnings; use strict; while(<DATA>){ printf("%d %04.2f %3d\n",split()) } __DATA__ 1 0.4 2 1 0.2 -5 1 0.1 -18 1 0.11 7 0 0.13 10 0 0.22 11
    Produces:
    1 0.40 2 1 0.20 -5 1 0.10 -18 1 0.11 7 0 0.13 10 0 0.22 11
    Or maybe investigate format.
Re: Keeping one colmun fixed and second column changed at different intervals to print the output
by chrestomanci (Priest) on Nov 30, 2010 at 13:54 UTC

    My first reply went missing, so here it is again:

    I think you need to clarify what you are trying to achieve. Perhaps by giving an example of the output you want as well as the input. You should also tidy your code, so that it is easer to read, as the indenting makes it hard to follow.

    Your code also contains some strange features, such as where you read the whole line into the array @list, join it to the scalar $f and then split it again into another array @t. It makes no sense. Better still would be to read and process your file linewise as part of the foreach loop.

    You need to use strict and warnings. Always and everywhere.

    Another improvement would be to name the fields when your split each line instead of using array indexes, as that will make the code easier to read, and help your understanding of what you are doing. If you do that then:

    my @x = split("\t",$_);

    Becomes:

    my($index, $key, $data, @rest) = split("\t",$_);

    The if statements further down then become more readable. (Obviously you would need to choose your own variable names to match what you are doing. I just guessed some likely looking names.)

    Modifying your code with these suggestions gives us:

    #!/usr/bin/perl -w use strict; use warnings; open (FILE,"$ARGV[0]") or die; open (WRITE,">ca_0.01_1.txt"); while( my $line = <FILE> ) { my($index, $key, $data, @rest) = split("\t",$line); if(($key<=0.01) && ($index=="0")) { next if ( $data>=-8); next if ($data<-9); #next if ($index!=0); #next if ($index!=1); print WRITE ($index, $key, $data, @rest),"\n"; } } close (WRITE); close (FILE); exit;

    You still need to clarify what you are doing, because it still makes no sense.

      Question is we have three columns first one column is 0 or 1 second column are some values of A and third column are some values of B

      for a given condtion for 0.01 interval of A values and B values interval of -9 to +9 say at A threshold =0.01 and B at threshold =-9 if value in the second column of A > A (threshold) and value in the third column B > B value (threshold) then for that output value is 1

      or if either of them is greater than threshold and then output value is 1 if both of them less than threshold then 0 .

      Likewise at different thresholds keeping one threshold fixed other say gab changes from -9 to -8 . then do calculation.

        #!/usr/bin/perl -w use strict; use warnings; open (FILE,"$ARGV[0]") or die; #open (WRITE,">aa"); #close (FILE); while( my $line = <FILE> ) { my($index, $key, $data, @rest) = split("\t",$line); for my ( $x=0.01,$x<=1;$x++) { for my ( $y=-9;$y<=9;$y++) { if (($key >$x) || ($data >$y)) { print "1","\n"; $x++; $y++; } else { print "0","\n +"; } } } } close (FILE);
        what is the error in the code