zarath has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Splitting big file or merging specific files?
by prysmatik (Sexton) on Jun 29, 2017 at 18:25 UTC | |
|
Re: Splitting big file or merging specific files?
by tybalt89 (Monsignor) on Jun 29, 2017 at 23:15 UTC | |
|
Re: Splitting big file or merging specific files?
by GrandFather (Saint) on Jun 30, 2017 at 04:05 UTC | |
|
Re: Splitting big file or merging specific files?
by thanos1983 (Parson) on Jun 29, 2017 at 16:57 UTC | |
|
Re: Splitting big file or merging specific files?
by zarath (Beadle) on Jun 30, 2017 at 09:00 UTC |