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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:59:25+00:00 2026-06-07T09:59:25+00:00

i’m working on a simple loop but it’s not working. I have 2 files

  • 0

i’m working on a simple loop but it’s not working. I have 2 files which i compare based on a common id. It sort of works because it outputs the first result and the first result only! so it’s not looping through the rest of the files (or last file because the output contains all the lines from the first file). It outputs all the lines from the first file but it only appends the first result from the second file into the outfile. Here is my code to clarify:

use strict;
use warnings;

my $inputfile1 = shift || die "Give input & output!\n";
my $inputfile2 = shift || die "Give input & output!\n";
my $outputfile = shift || die "Give input!\n";

open my $INFILE1,  '<', $inputfile1 or die "In use / Not found :$!\n";
open my $INFILE2,  '<', $inputfile2 or die "In use / Not found :$!\n";
open my $OUTFILE,  '>', $outputfile or die "In use / Not found :$!\n";

while (<$INFILE1>) {
s/"//g;
my @elements = split /;/, $_;

    while (<$INFILE2>) {
        s/"//g;
        my @loopelements = split /;/, $_;

            if ($elements[11] eq $loopelements[0]){
                $elements[12] = $loopelements[1];
                $elements[13] = $loopelements[2];
                }
    }

my $output_line = join(";", @elements);
print $OUTFILE $output_line;
#print "\n"
}

close $INFILE1;
close $INFILE2;
close $OUTFILE;

exit 0;

My first attempt was this piece of code, and it worked partially. Why?: halfway through it crashes. When i check the outputfile it worked to about halfway and stops. No idea why! And on a side note, i think the one below is less efficiënt or is there a better alternative for both?

$inputfile1=$ARGV[0];  
$inputfile2=$ARGV[1]; 
$outputfile1=$ARGV[2];

open(INFILE1,$inputfile1) || die "Give input & output :$!\n";
open(INFILE2,$inputfile2) || die "Give input & output :$!\n";
open(OUTFILE_1,">$outputfile1") || die "Give input & output :$!\n";

$i = 0;
$j = 0;

@infile1=<INFILE1>;
@infile2=<INFILE2>;

foreach ( @infile1 )
  {
  @elements = split(";",$infile1[$i]);
  $j=0;

  foreach ( @infile2 )
      {
      @loopelements = split(";",$infile2[$j]);

      if ($elements[11] eq $loopelements[0]){
         $elements[12] = $loopelements[1];
         $elements[13] = $loopelements[2];
         $printen = 1;
         last;
        }

      $j = $j+1;
      }

  @elements = join(";",@elements);
  print "$i\r";
  if ($printen == 1) { print OUTFILE_1 "@elements"; };

  $i = $i+1;
  }
close(INFILE1);
close(INFILE2);
close(OUTFILE_1); 

So can someone point out where i’m going wrong in my code at the top?

  • 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-07T09:59:27+00:00Added an answer on June 7, 2026 at 9:59 am
    1. The first line of first file is read in the first iteration of outer loop.

    2. During this first iteration itself, all the lines of second file are read in the inner loop.

    3. Then the first iteration of outer loop ends.

    4. Now, second iteration of outer loop comes up. Are there any lines of second file remaining to read anymore? No.

    Breaking the problem down to its simplest, the two lines with comments next to them make the program loop through the lines of the second file each time:

    use warnings;
    
    my $inputfile1 = shift || die "Give input & output!\n";
    my $inputfile2 = shift || die "Give input & output!\n";
    
    open my $INFILE1,  '<', $inputfile1 or die "In use / Not found :$!\n";
    open my $INFILE2,  '<', $inputfile2 or die "In use / Not found :$!\n";
    
    my $infile2_pos = tell $INFILE2; # remember start position
    
    while (<$INFILE1>) {
    
      print;
    
      seek $INFILE2, $infile2_pos, 0; # seek the start position
    
      while (<$INFILE2>) {
        print;
      }
    }
    

    If this is too slow, I see two things you can do:

    1. Read the bigger file in the outer loop (I guess you know why this will speed it up).
    2. Read the smaller file initially into an array, so you don’t have to do disk I/O repeatedly on it.

    What I mean by this is:

    open my $BIGFILE,  '<', $bigfile or die "In use / Not found :$!\n";
    open my $SMALLFILE,  '<', $smallfile or die "In use / Not found :$!\n";
    
    my @smallfile_array = <$SMALLFILE>;
    
    while (<$BIGFILE>) {
    
      print;
    
      foreach (@smallfile_array) {
        print;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.