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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:38:45+00:00 2026-05-14T01:38:45+00:00

I have two text files that contain columnar data of the variety position –

  • 0

I have two text files that contain columnar data of the variety position–value, sorted by position.

Here is an example of the first file (file A):

100   1
101   1
102   0
103   2
104   1
...

Here is an example of the second file (B):

20    0
21    0
...
100   2
101   1
192   3
193   1
...

Instead of reading one of the two files into a hash table, which is prohibitive due to memory constraints, what I would like to do is walk through two files simultaneously, in a stepwise fashion.

What this means is that I would like to stream through lines of either A or B and compare position values.

If the two positions are equal, then I perform a calculation on the values associated with that position.

Otherwise, if the positions are not equal, I move through lines of file A or file B until the positions are equal (when I again perform my calculation) or I reach EOF of both files.

Is there a way to do this in Perl?

  • 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-14T01:38:46+00:00Added an answer on May 14, 2026 at 1:38 am

    Looks like a problem one would likely stumble upon, for example database table data with keys and values. Here’s an implementation of the pseudocode provided by rjp.

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    sub read_file_line {
      my $fh = shift;
    
      if ($fh and my $line = <$fh>) {
        chomp $line;
        return [ split(/\t/, $line) ];
      }
      return;
    }
    
    sub compute {
       # do something with the 2 values
    }
    
    open(my $f1, "file1");
    open(my $f2, "file2");
    
    my $pair1 = read_file_line($f1);
    my $pair2 = read_file_line($f2);
    
    while ($pair1 and $pair2) {
      if ($pair1->[0] < $pair2->[0]) {
        $pair1 = read_file_line($f1);
      } elsif ($pair2->[0] < $pair1->[0]) {
        $pair2 = read_file_line($f2);
      } else {
        compute($pair1->[1], $pair2->[1]);
        $pair1 = read_file_line($f1);
        $pair2 = read_file_line($f2);
      }
    }
    
    close($f1);
    close($f2);
    

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have two text files that each contain different information about certain structures.The
I have two large (~100 GB) text files that must be iterated through simultaneously.
I have 2 plain text files that contain some words, like: File 1 Aarhus
I have an asp page (Default.aspx) that displays the diff between two text files.
So I have two files, one that contains my text, and another which I
I have two text files which contain the below numbers File1 00000 11111 File2
I have two text files in two different languages and they are aligned line
I have two text files. hash_only.txt and final_output.txt hash_only.txt looks like below. 193548 401125
I have two text files with various columns, each column is separated by a
I have two text files, I want to place a text in the middle

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.