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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:10:54+00:00 2026-06-18T06:10:54+00:00

i have 2 files each having 50 lines.. FILE1 FILE2 now, i need to

  • 0

i have 2 files each having 50 lines..

FILE1
FILE2

now, i need to read two file lines by line in a single while or for loop and i should push the corresponding line to the 2 output arrays. i have tried something like this. but its not working out. kindly help

#!/usr/bin/perl

   my @B =();
   my @C =();
   my @D =();
   my $lines = 0;
   my $i = 0;
   my $sizeL = 0;
   my $sizeR = 0;
  my $gf = 0; 
  $inputFile = $ARGV[0];
  $outputFile = $ARGV[1];
  open(IN1FILE,"<$inputFile") or die "cant open output file ";
  open(IN2FILE,"<$outputFile") or die "cant open output file";

  while((@B=<IN1FILE>)&&(@C= <IN2FILE>))
  {
  my $line1 = <IN1FILE>;
  my $line2 = <IN2FILE>;
  print $line2;
  }

Here array 2 is not getting build.. but i am getting array 1 value.

  • 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-18T06:10:56+00:00Added an answer on June 18, 2026 at 6:10 am

    In your loop condition, you read the whole files into their arrays. The list assignment is then used as a boolean value. This works only once, as the files will be read after the condition has been evaluated. Also, the readlines inside the loop will return undef.

    Here is code that should work:

    my (@lines_1, @lines_2);
    # read until one file hits EOF
    while (!eof $INFILE_1 and !eof $INFILE_2) {
      my $line1 = <$INFILE_1>;
      my $line2 = <$INFILE_2>;
      say "from the 1st file: $line1";
      say "from the 2nd file: $line2";
      push @lines_1, $line1;
      push @lines_2, $line2;
    }
    

    You could also do:

    my (@lines_1, @lines_2);
    # read while both files return strings
    while (defined(my $line1 = <$INFILE_1>) and defined(my $line2 = <$INFILE_2>)) {
      say "from the 1st file: $line1";
      say "from the 2nd file: $line2";
      push @lines_1, $line1;
      push @lines_2, $line2;
    }
    

    Or:

    # read once into arrays
    my @lines_1 = <$INFILE_1>;
    my @lines_2 = <$INFILE_2>;
    my $min_size = $#lines_1 < $#lines_2 ? $#lines_1 : $#lines_2; # $#foo is last index of @foo
    # then interate over data
    for my $i ( 0 .. $min_size) {
      my ($line1, $line2) = ($lines_1[$i], $lines_2[$i]);
      say "from the 1st file: $line1";
      say "from the 2nd file: $line2";
    }
    

    Of course, I am assuming that you did use strict; use warnings; and use feature 'say', and used the 3-arg form of open with lexical filehandles:

    my ($file_1, $file_2) = @ARGV;
    open my $INFILE_1, '<', $file_1 or die "Can't open $file_1: $!"; # also, provide the actual error!
    open my $INFILE_2, '<', $file_2 or die "Can't open $file_2: $!";
    

    I also urge you to use descriptive variable names instead of single letters, and to declare your variables in the innermost possible scope — declaring vars at the beginning is almost the same as using bad, bad globals.

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

Sidebar

Related Questions

I have a *.txt files having integers, one on each line. So the file
I have two files A - nodes_to_delete and B - nodes_to_keep . Each file
I have a text corpus of 11 files each having about 190000 lines. I
I have a long text file having truck configurations. In each line some properties
I have a normal text files with each line having data in the following
I have a file wich has about 12 millon lines, each line looks like
I have two files independant on each other. Let's just call it Class1 and
I have set of flat files (114 files) each file is named with database
Hi I have two text files that each contain different information about certain structures.The
i have two files:(localhost/template/) index.php template.php each time when i create an article(an article

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.