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

  • Home
  • SEARCH
  • 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 8981353
In Process

The Archive Base Latest Questions

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

I am reviewing for a test and I can’t seem to get this example

  • 0

I am reviewing for a test and I can’t seem to get this example to code out right.

Problem: Write a perl script, called ileaf, which will linterleave the lines of a file with those of another file writing the result to a third file. If the files are a different length then the excess lines are written at the end.

A sample invocation:
ileaf file1 file2 outfile

This is what I have:

#!/usr/bin/perl -w

open(file1, "$ARGV[0]");
open(file2, "$ARGV[1]");
open(file3, ">$ARGV[2]");

while(($line1 = <file1>)||($line2 = <file2>)){
    if($line1){
            print $line1;
    }

    if($line2){
            print $line2;
    }
}

This sends the information to screen so I can immediately see the result. The final verson should “print file3 $line1;” I am getting all of file1 then all of file2 w/out and interleaving of the lines.

If I understand correctly, this is a function of the use of the “||” in my while loop. The while checks the first comparison and if it’s true drops into the loop. Which will only check file1. Once file1 is false then the while checks file2 and again drops into the loop.

What can I do to interleave the lines?

  • 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-15T20:18:56+00:00Added an answer on June 15, 2026 at 8:18 pm

    You’re not getting what you want from while(($line1 = <file1>)||($line2 = <file2>)){ because as long as ($line1 = <file1>) is true, ($line2 = <file2>) never happens.

    Try something like this instead:

    open my $file1, "<", $ARGV[0] or die;
    open my $file2, "<", $ARGV[1] or die;
    open my $file3, ">", $ARGV[2] or die;
    
    while (my $f1 = readline ($file1)) {
      print $file3 $f1;  #line from file1
    
      if (my $f2 = readline ($file2)) {  #if there are any lines left in file2
        print $file3 $f2;
      }
    }
    
    while (my $f2 = readline ($file2)) {   #if there are any lines left in file2
      print $file3 $f2;
    }
    
    close $file1;
    close $file2;
    close $file3;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When reviewing code I came across something odd, someone had read that you can
I'm reviewing for a test, and I am stumped by this question. Consider the
I am trying to write this script to my linux terminal and I am
I'm reviewing for a test, and I cant figure out why the answer for
I have some test code which is not working as I expect, after reviewing
Reviewing the MSDN docs for SmtpClient , I noticed that the example code looks
I am reviewing this MATLAB code ,and it have some examples, I am executing
After reviewing A LOT of questions and Internet data, I've solved a problem of
I'm reviewing some code on the project I recently joined, and in a C#
I'm reviewing some code from one of our contractors: if (userLists != null) {

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.