Hello,

My question is actually about 2 separate scripts, but I'm trying to do 1 thing: making files that consists of 2 specific lines each.

I'm pretty new at Perl and this assignment is very different from what I have done with it in the past. I can't get either code to do exactly what I want, so I have no idea which code to keep working on to get to the solution fastest. Hope you guys can help me out.

I have a file with around 3000 lines in it. With this I need to create about 1500 separate files, each containing 2 lines. The lines in outputfile 1 should be line 1 and 2 from the inputfile; the lines in outputfile 2 should be line 3 and 4 from the inputfile and so on... So each line has to be written exactly once (no more, no less) and I have to be sure the order of the lines written is respected. The code I have written to work this way splits up the big file in small files, but it only writes 1 line to each file.

I have tried using File::CountLines to make the loop stop at 2 lines and open a new outputfile, but this only confused myself... the linecounter seemed to be stuck on either 0 or 1, writing the same line to an infinite number of files, or even worse: kept filling the same file in an endless loop, making the file too big to open very quickly. So I quickly threw this option out the window.

If this is the code I need to work on, this is it:

#!perl use strict; use warnings; use autodie; # die if problem reading or writing a file use feature qw(say); my $input = "C:/Some/specific/path/to/file.txt"; open FILEin, $input; my @input = <FILEin>; binmode(FILEin); undef $/; foreach $input (@input) { my $dir = 'C:/Some/specific/folder'; my @files = <$dir/*>; my $countf = @files; my $outfile = $dir.'/bladibla_'.$countf.'.txt'; open FILEout, '>'.$outfile; print FILEout $input; close FILEout; say 'Wrote '.$input.' to '.$outfile; } close FILEin;

The other code is an attempt at working the other way around.

I let the first code do what it does to end up with the 3000 files with each containing 1 line. The files contain a counter in the filename which is reliable, meaning: outputfile 1 should contain the contents of inputfile 0 and 1; outputfile 2 should contain the contents of inputfile 2 and 3 and so on...

The problem with the code I have written for this is that it doesn't seem to loop, it writes 1 file and then stops. The written file is correct (contains the contents of the first 2 inputfiles), but it should do this for all files found in the inputfolder. I know there is a problem with my 'for'-loop, but it seems I am not experienced enough to figure out what it should be.

If I better keep working on this code, here it is:

#!perl use strict; use warnings; use autodie; # die if problem reading or writing a file use feature qw(say); my $inbase = 'C:/Some/specific/folder'; my $outbase = 'C:/Some/other/specific/folder'; my $counter = @ARGV; for my $file ($inbase.'/bladibla_'.$counter.'.txt') { my @outfiles = <$outbase/*>; my $countf = @outfiles; my $outfile = $outbase.'/bladibla_'.$countf.'.txt'; open FILEout, '>'.$outfile; my $file = ($inbase.'/bladibla_'.$counter.'.txt'); open FILEin, $file; my $contents = <FILEin>; binmode(FILEin); print FILEout $contents; close FILEin; say 'Wrote '.$contents.' to '.$outfile; $counter += 1; my $file2 = ($inbase.'/bladibla_'.$counter.'.txt'); open FILEin, $file2; my $contents2 = <FILEin>; binmode(FILEin); print FILEout $contents2; close FILEin; say 'Wrote '.$contents2.' to '.$outfile; close FILEout; $counter += 1; }

I'm sure both codes can be tweaked to do exactly what I need it to do, just pick the one you think needs the least/easiest work.

Thank you very much in advance!


In reply to Splitting big file or merging specific files? by zarath

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.