Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8044353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:12:13+00:00 2026-06-05T05:12:13+00:00

I am back with another question. I have a list of data: 1 L

  • 0

I am back with another question. I have a list of data:

1 L DIELTQSPE H EVQLQESDAELVKPGASVKISCKASGYTFTDHE
2 L DIVLTQSPRVT H EVQLQQSGAELVKPGASIKDTY
3 A ALQLTQSPSSLSAS B RITLKESGPPLVKPTCS C ELDKWAN
4 A ALQLTQSPSSLSAS B RITLKESGPPLVKPTCS C ELDKWAG
5 A ALQLTQSPSSLSAS B RITLKESGPPLVKPTCS C LELDKWASL
6 L DIQMTQIPSSLSASLSIC H EVQLQQSGVEVKMSCKASGYTFTS
7 L SYELTQPPSVSVSPGSIT H QVQLVQSAKGSGYSFS P YNKRKAFYTTKNIIG
8 L SYELTQPPSVSVSPGRIT H EVQLVQSGAASGYSFS P NNTRKAFYATGDIIG
9 A MPIMGSSVAVLAIL B DIVMTQSPTVTI C EVQLQQSGRGP
10 A MPIMGSSVVLAIL B DIVMTQSPTVTI C EVQLQQSGRGP
11 L DVVMTQTPLQ H EVKLDESVTVTSSTWPSQSITCNVAHPASSTKVDKKIE
12 A DIVMTQSPDAQYYSTPYSFGQGTKLEIKR

And I would like to compare the 3rd elements && 5th elements of each row, then group them if they have the same 3rd && 5th elements.
For example, with the data above, the results will be :

3: 3 A ALQLTQSPSSLSAS B RITLKESGPPLVKPTCS C ELDKWAN
   4 A ALQLTQSPSSLSAS B RITLKESGPPLVKPTCS C ELDKWAG
   5 A ALQLTQSPSSLSAS B RITLKESGPPLVKPTCS C LELDKWASL
9: 9 A MPIMGSSVAVLAIL B DIVMTQSPTVTI C EVQLQQSGRGP
   10 A MPIMGSSVVLAIL B DIVMTQSPTVTI C EVQLQQSGRGP

Fyi, in the actual data, the 3rd, 5th, 7th elements are very long. I have made them cut to see the whole.

This is what I have done, I know it is very clumsy, but as a beginner, I am doing my best.
And the problem is that it shows only the first set of ‘same’ group.
Could you show me where it went wrong and/or other pretty methods to solve this, please?

my $file = <>;
open(IN, $file)|| die "no $file: $!\n";
my @arr;
while (my $line=<IN>){
        push @arr, [split (/\s+/, $line)] ;
}
close IN;

my (@temp1, @temp2,%hash1);
for (my $i=0;$i<=$#arr ;$i++) {
    push @temp1, [$arr[$i][2], $arr[$i][4]]; 
    for (my $j=$i+1;$j<=$#arr ;$j++) {
        push @temp2, [$arr[$j][2], $arr[$j][4]];
        if (($temp1[$i][0] eq $temp2[$j][0])&& ($temp1[$i][1] eq $temp2[$j][1])) {
            push @{$hash1{$arr[$i][0]}}, $arr[$i], $arr[$j];
        }
    }
}
print Dumper \%hash1;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:12:16+00:00Added an answer on June 5, 2026 at 5:12 am

    You appear to have overcomplicated this a bit more than it needs to be, but that’s common for beginners. Think more about how you would do this manually:

    • Look at each line.
    • See whether the third and fifth fields are the same as the previous line.
    • If so, print them.

    The looping and all that is completely unnecessary:

    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    
    my ($previous_row, $third, $fifth) = ('') x 3;
    
    while (<DATA>) {
      my @fields = split;
      if ($fields[2] eq $third && $fields[4] eq $fifth) {
        print $previous_row if $previous_row;
        print "\t$_";
        $previous_row = '';
      } else {
        $previous_row = $fields[0] . "\t" . $_;
        $third = $fields[2];
        $fifth = $fields[4];
      }
    }
    
    __DATA__
    1 L DIELTQSPE H EVQLQESDAELVKPGASVKISCKASGYTFTDHE
    2 L DIVLTQSPRVT H EVQLQQSGAELVKPGASIKDTY
    3 A ALQLTQSPSSLSAS B RITLKESGPPLVKPTCS C ELDKWAN
    4 A ALQLTQSPSSLSAS B RITLKESGPPLVKPTCS C ELDKWAG
    5 A ALQLTQSPSSLSAS B RITLKESGPPLVKPTCS C LELDKWASL
    6 L DIQMTQIPSSLSASLSIC H EVQLQQSGVEVKMSCKASGYTFTS
    7 L SYELTQPPSVSVSPGSIT H QVQLVQSAKGSGYSFS P YNKRKAFYTTKNIIG
    8 L SYELTQPPSVSVSPGRIT H EVQLVQSGAASGYSFS P NNTRKAFYATGDIIG
    9 A MPIMGSSVAVLAIL B DIVMTQSPTVTI C EVQLQQSGRGP
    10 A MPIMGSSVAVLAIL B DIVMTQSPTVTI C EVQLQQSGRGP
    11 L DVVMTQTPLQ H EVKLDESVTVTSSTWPSQSITCNVAHPASSTKVDKKIE
    12 A DIVMTQSPDAQYYSTPYSFGQGTKLEIKR
    

    (Note that I changed line 10 slightly so that its third field will match line 9 in order to get the same groups in the output as specified.)

    Edit: One line of code was duplicated by a copy/paste error.

    Edit 2: In response to comments, here’s a second version which doesn’t assume that the lines which should be grouped are contiguous:

    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    
    my @lines;
    while (<DATA>) {
      push @lines, [ $_, split ];
    }
    
    # Sort @lines based on third and fifth fields (alphabetically), then on
    # first field/line number (numerically) when third and fifth fields match
    @lines = sort { 
      $a->[3] cmp $b->[3] || $a->[5] cmp $b->[5] || $a->[1] <=> $b->[1] 
    } @lines;
    
    my ($previous_row, $third, $fifth) = ('') x 3;
    for (@lines) {
      if ($_->[3] eq $third && $_->[5] eq $fifth) {
        print $previous_row if $previous_row;
        print "\t$_->[0]";
        $previous_row = '';
      } else {
        $previous_row = $_->[1] . "\t" . $_->[0];
        $third = $_->[3];
        $fifth = $_->[5];
      }
    }
    
    __DATA__
    1 L DIELTQSPE H EVQLQESDAELVKPGASVKISCKASGYTFTDHE
    3 A ALQLTQSPSSLSAS B RITLKESGPPLVKPTCS C ELDKWAN
    2 L DIVLTQSPRVT H EVQLQQSGAELVKPGASIKDTY
    5 A ALQLTQSPSSLSAS B RITLKESGPPLVKPTCS C LELDKWASL
    7 L SYELTQPPSVSVSPGSIT H QVQLVQSAKGSGYSFS P YNKRKAFYTTKNIIG
    6 L DIQMTQIPSSLSASLSIC H EVQLQQSGVEVKMSCKASGYTFTS
    9 A MPIMGSSVAVLAIL B DIVMTQSPTVTI C EVQLQQSGRGP
    8 L SYELTQPPSVSVSPGRIT H EVQLVQSGAASGYSFS P NNTRKAFYATGDIIG
    11 L DVVMTQTPLQ H EVKLDESVTVTSSTWPSQSITCNVAHPASSTKVDKKIE
    10 A MPIMGSSVAVLAIL B DIVMTQSPTVTI C EVQLQQSGRGP
    12 A DIVMTQSPDAQYYSTPYSFGQGTKLEIKR
    4 A ALQLTQSPSSLSAS B RITLKESGPPLVKPTCS C ELDKWAG
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm back with another Flex/Flash security question. I've already received some help from the
Back-end: I have a model (User) that has_many of another model (ContactPreference). Front-end: An
Y.A.N.Q. (yet another n00b question). I have managed to design my db, defining tables
hello all im back with another question on my project. i keep getting stuck
I'm back with another question regarding Android databases. I'm storing a simple string into
I am starting another activity by calling startActivityForResult() and after pressing back button my
I've got 10 GB of files to back up daily to another site. The
This question brings me back to my college days, but since I haven't coded
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
I have a database where I store two different kinds of data. One table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.