in reply to max and min values in 3-column file

Another way to do this by reading the file a line at a time:

$ echo "Column1 Column2 Column3 2870 S11 1 574 S11 2 317 S11 3 31 S11 4 1 S11 6 1 S11 7 2925 S12 1 8 S12 5 1 S12 6 1 S12 9 " | perl -e' use warnings; use strict; my ( @lowest, @highest ); while ( <> ) { next if /^\D/; my @fields = split; unless ( @lowest ) { @lowest = @highest = @fields; next; } if ( $lowest[ -1 ] >= $fields[ -1 ] && $lowest[ 0 ] < $fields[ 0 ] + ) { @lowest = @fields; } if ( $highest[ -1 ] < $fields[ -1 ] ) { @highest = @fields; } } print join( "\t", "Lowest:", @lowest ), "\n"; print join( "\t", "Highest:", @highest ), "\n"; ' Lowest: 2925 S12 1 Highest: 1 S12 9

Replies are listed 'Best First'.
Re^2: max and min values in 3-column file
by perldummie (Initiate) on Feb 06, 2011 at 14:31 UTC
    Thanks for all of your replies. Yes, column 1 shows the frequency of the value in column 1. Could you please show me how to load my txt.file into your script, so that I can actually try it. I tried to do it the way I did it with my last script, but it somehow doesn't work. And as mentioned I am such a beginner in Perl that I don't know any other ways of how to upload txt files yet... Thanks so much to all of you for trying to help me!
      If you see a loop like
      while (<>){ ... }
      then it means that the script is reading from STDIN (standard input). You now need to find out how to compose a command line that passes your file on STDIN to the perl script, which is a nice little exercise... :)
      One of the solutions posted here used a DATA block at the end of the script, i.e. there is a signal to tell the perl interpreter that the rest of the file is not Perl code but data and the while loop reads from the data block line by line instead of STDIN.