Let's Get Together
AUTHOR
David Romano
In this event, you need to combine the contents of several text files into one. There are a number of things you need to do in order to succeed at this event:
1.
Find all the text files (files with a .txt file extension) in the folder C:\Scripts. (Make sure you use C:\Scripts; if you use any other folder you wonโt receive the points for this event.)
2.
Create a new file named C:\Temp\Newfile.txt. (Once again, the full path and name for the file your script creates must match this exactly.)
3.
Copy the first line โ and only the first line โ from each text file in C:\Scripts into your new file.
Thatโs it. When your script completes you should have a new file in your C:\Temp folder named Newfile.txt. Newfile.txt should contain the first line โ followed by a carriage-return linefeed โ from each text file in C:\Scripts.
use v6;
my $run-dir = $*PROGRAM-NAME.IO.dirname;
my @files = dir($run-dir).sort;
my $output = $*SPEC.catdir($run-dir, 'newfile.txt');
$output.IO.unlink if $output.IO.e;
# only select .txt files required for this event
for @files.grep: { .match(/test.*\.txt $$/) } {
my $inputfh = open $_, :r;
my $outputfh = open $output, :a;
$outputfh.say( $($inputfh.get) ); # $(...) forces item context
.close for $inputfh, $outputfh;
}
# vim: expandtab shiftwidth=4 ft=perl6