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

ORF                                        PHAGE_NAME              START     END     Hvalue
***************************************************************************************************************
orf00007                    PHAGE_Prochl_MED4_213_NC_020845-gi|472340344|ref|YP_007673870.1|     7665 8618    0.210897481636936
orf00007                    PHAGE_Prochl_P_HM2_NC_015284-gi|326783200|ref|YP_004323597.1|        7665 8618    0.207761175236097
orf00007                    PHAGE_Prochl_P_HM1_NC_015280-gi|326782251|ref|YP_004322652.1|        7665 8618    0.207761175236097
orf00007                    PHAGE_Prochl_Syn1_NC_015288-gi|326784174|ref|YP_004324567.1|         7665 8618    0.176257712486884
orf00007                    PHAGE_Cyanop_P_RSM6_NC_020855-gi|472341546|ref|YP_007675062.1|       7665 8618    0.201443231899265
orf00007                    PHAGE_Synech_syn9_NC_008296-gi|113200706|ref|YP_717869.1|            7665 8618    0.188906505771249



1. ignore 1st and 2nd line
2. Take colum one and compare the row 3 and 4 it should be equal
3. Take colum two and comapre the row 3 and 4 it should be equal
4. then check colum 4 and check row 3 and 4 greater value, if found the greater value check with the next row and print greater value of the same group.


The code i had tried for it is
#!/usr/bin/perl
#use strict;
use warnings;

open FILE,"out07.txt";

my @fields = split / /, <FILE>;
chomp @fields;
my @lines = split (/(\n)/, "@fields");
for (@lines)
{
    print @fields,"\n";
    
} 
  • Comment on Sorting using the highest last column criteria

Replies are listed 'Best First'.
Re: Sorting using the highest last column criteria
by AppleFritter (Vicar) on Jul 27, 2014 at 17:48 UTC

    2. Take colum one and compare the row 3 and 4 it should be equal
    3. Take colum two and comapre the row 3 and 4 it should be equal

    What if they aren't, like in your example? The second fields in row 3 and 4 are, respectively, "PHAGE_Prochl_MED4_213_NC_020845-gi|472340344|ref|YP_007673870.1|" and "PHAGE_Prochl_P_HM2_NC_015284-gi|326783200|ref|YP_004323597.1|": not the same.

    Or do you want to compare other fields, and not PHAGE_NAME?

    4. then check colum 4 and check row 3 and 4 greater value, if found the greater value check with the next row and print greater value of the same group.

    Could you clarify what you mean by "the same group" -- the entire file? (And BTW, I assume that you want continue this list-folding process beyond those rows, right?)

    It'd be good if you could share, at the minimum, the expected output for your example data.

Re: Sorting using the highest last column criteria
by Bethany (Scribe) on Jul 27, 2014 at 15:22 UTC

    What have you tried so far, and what results did you get?

    You'll get much better replies if you show what effort you've already put into solving your problem, how far you've got toward a solution, and what parts you don't understand. This is especially true when it comes to help with homework assignments. It's also helpful to let others know how much experience you have with Perl, from "none at all" to "I've read a quick introduction and written a few simple scripts."

    The more (relevant) information you provide, the better others will be able to guide you.

    (edited to add) Oh, and the Anonymous Monk above is right -- you should definitely check out the Guide to the Monastery. I think this part of the FAQ is the most important for newcomers: How do I post a question effectively?

      #!/usr/bin/perl #use strict; use warnings; open FILE,"out07.txt"; my @fields = split / /, <FILE>; chomp @fields; my @lines = split (/(\n)/, "@fields"); for (@lines) { print @fields,"\n"; }

        Okay, that's a start. I see some problems that you should be able to fix.

        When I'm not sure why a script isn't doing what I expect, a good exercise is to write a comment before each line telling what I think that line of code should do. If it's a complicated line I write a comment for each part of the line. (This is a variation on "rubber duck debugging".)

        Then I add debugging code to print out the results after each step. This way I can find where things stop behaving as I expect them to behave.

        You won't learn much if someone just tells you "do this instead" and provides a ready-to-use solution. If you work it out for yourself you will have learned not only how to fix this problem but also how to approach the next problem you have to solve.

        Past this advice, as AppleFritter pointed out, you'll need to understand what you want your results to look like -- and you'll have to tell this to the people you're asking for guidance. Post an example of the output you'd like your code to produce.

Re: Sorting using the highest last column criteria
by Anonymous Monk on Jul 27, 2014 at 15:13 UTC