If one input file is shorter than the other, it should return to the first line, which is the difficulty to me.

If you store the input files as arrays then it is less tricky:

#!/usr/bin/env perl #===================================================================== +========== # # FILE: zip.pl # # USAGE: ./zip.pl input1 input2 output # # DESCRIPTION: Zip 2 input files into one output file, looping the # shorter one. # # REQUIREMENTS: Path::Tiny # NOTES: See http://www.perlmonks.org/?node_id=1191418 #===================================================================== +========== use strict; use warnings; use Path::Tiny; my @in = ( path ($ARGV[0]), path ($ARGV[1]) ); my $out = $ARGV[2]; my @first = $in[0]->lines; my @second = $in[1]->lines; my $max = $#first > $#second ? $#first : $#second; open my $outfh, '>', $out or die "Cannot open $out for writing: $!"; for (0 .. $max) { print $outfh $first[$_ % @first] . $second[$_ % @second] }

Bad argument trapping is left as an exercise.


In reply to Re: writing two files (different in length) to one output by hippo
in thread writing two files (different in length) to one output by ic23oluk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.