Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Merging files, 1 line for every 10

by Kc12349 (Monk)
on Sep 06, 2011 at 22:45 UTC ( #924479=note: print w/ replies, xml ) Need Help??


in reply to Merging files, 1 line for every 10

I'm with others here wondering what possible purpose this serves, but I would probably do something like the below. $in_file1 being the larger of the two files.

open($in_fh1, '<', $in_file1); open($in_fh2, '<', $in_file2); open($out_fh, '>', $out_file); while (my $line1 = <$in_fh1>) { chomp($line1); state $i; $i++; say {$out_fh} $line1; unless ($i % 10) { chomp(my $line2 = <$in_fh2>); say {$out_fh} $line2 if $line2; } }


Comment on Re: Merging files, 1 line for every 10
Download Code
Re^2: Merging files, 1 line for every 10
by trizen (Friar) on Sep 07, 2011 at 21:59 UTC
    I'd recommend a simpler code:
    open my $in_fh1, '<', $in_file1; open my $in_fh2, '<', $in_file2; open my $out_fh, '>', $out_file; while (defined(my $line1 = <$in_fh1>)) { print {$out_fh} $line1; unless ($. % 10) { my $line = <$in_fh2>; print {$out_fh} $line; } }

      The use of $. does make things more simple. I'll have to take note of that for similar situations in the future. What is the benefit of the added defined call?

        It deals with the edge case where $line gets a value that is false before the actual end of file.

        True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://924479]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-09-22 05:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (180 votes), past polls