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 7598507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:25:24+00:00 2026-05-30T22:25:24+00:00

I have a file with 3 columns -> A1 0 9 A1 4 14

  • 0

I have a file with 3 columns ->

A1  0   9
A1  4   14
A1  16  24
A1  25  54
A1  64  84
A1  74  84
A2  15  20
A2  19  50

I want to check if each line (value in col2 and 3) is present already or is in between the range of previous line, if col1 value is equal.
The desired output is ->

A1  0   14
A1  16  54
A1  64  84
A2  15  50

I have tried ->

@ARGV or die "No input file specified";
open $first, '<',$ARGV[0] or die "Unable to open input file: $!";
#open $second,'<', $ARGV[1] or die "Unable to open input file: $!";
$k=0;
while (<$first>) 
{
if($k==0)
{
@cols = split /\s+/;
$p0=$cols[0];
$p1=$cols[1];
$p2=$cols[2];
$p3=$cols[2]+1;
}

else{
@new = split /\s+/;
if ($new[0] eq $p0){
    if ($new[1]>$p3)
        {
    print join("\t", @new),"\n";
    $p0=$new[0];
    $p1=$new[1];
    $p2=$new[2];
    $p3=$new[2]+1;


        }
    elsif ($new[2]>=$p2) 
    {
    print $p0,"\t",$p1,"\t",$new[2],"\n";
    $p2=$new[2];
    $p3=$new[2]+1;
    }

    else 
        {
    $p5=1;

        }   
}

      else 
      {
    print join("\t", @new),"\n";
        $p0=$new[0];
    $p1=$new[1];
    $p2=$new[2];
    $p3=$new[2]+1;

      }}
      $k=1;

}

and output I am getting is ->

A1    0       14
A1    16      24
A1    16      54
A1    64      84
A1    64      84
A2    15      20
A2    22      50

I am not able to understand why I am getting this wrong output. Also if there is any way that I can erase(or overwrite) the last printed line, then it will be very easy.

  • 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-05-30T22:25:25+00:00Added an answer on May 30, 2026 at 10:25 pm

    First of all, it would be much more simple to help you if you

    • used strict and warnings, and declared all your variabled close to first use with my
    • indented your code properly to show the structure

    The reason your code fails is that you are printing data under too many conditions. For example you output A1 16 24 when you find it cannot be joined with the previous range A1 4 14 without waiting for it to be extended by the subsequent A1 25 54 (when you correctly extend the range and print it again). A1 64 84 is output twice for the same reason: first because it cannot be merged with A1 25 54, and again because it has been “extended” with A1 74 84. Finally A2 15 20 is output straight away because it has a new first column, even though it is merged with the next line and output again.

    You need to output a range only when you have found that it cannot be extended again. That happens when

    • a new record is found that doesn’t overlap the existing data
    • the end of the file is reached

    This code prints output only in those cases an appears to do what you need.

    use strict;
    use warnings;
    
    my @data;
    
    while (<DATA>) {
    
      if (not @data) {
        @data = split;
        next;
      }
    
      my @new = split;
    
      if ($new[0] eq $data[0] and $new[1] <= $data[2] + 1) {
        $data[2] = $new[2];
      }
      else {
        print join("\t", @data), "\n";
        @data = @new;
      }
    
      print join("\t", @data), "\n" if eof DATA;
    
    }
    
    __DATA__
    A1  0   9
    A1  4   14
    A1  16  24
    A1  25  54
    A1  52  57
    A1  59  62
    A1  64  84
    A1  74  84
    A2  15  20
    A2  19  50
    

    OUTPUT

    A1  0 14
    A1  16  57
    A1  59  62
    A1  64  84
    A2  15  50
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ARFF file containing 14 numerical columns. I want to perform a
I have about 600 text files. Each file contains 2 columns and is space
I have a file with large number of columns. As a sanity check, I
If I have an XML file and I want to check the uniqueness of
I have a txt file with columns separated by tabs and based on that
I have a file which has multiple columns, whitespace separated. e.g: data1 data2 data3
i have an input csv file with columns position_Id, Asofdate, etc which has to
I have a .csv file containing 3 columns of data. I need to create
I have .csv file that contain 2 columns delimited with , . file.csv word1,word2
I have a .csv file that contains data for only certain columns in a

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.