Skip to content

Instantly share code, notes, and snippets.

@MattOates
Created January 5, 2018 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattOates/0c54dd0152a5ca05b20f7dbd7b008591 to your computer and use it in GitHub Desktop.
Save MattOates/0c54dd0152a5ca05b20f7dbd7b008591 to your computer and use it in GitHub Desktop.
Explode records of files into new files based on the value of a given field
#!/usr/bin/env perl6
%*SUB-MAIN-OPTS<named-anywhere> = True;
my %out_files;
sub MAIN( @in-files where { .IO.f // die "Input file $_ not found" },
Int :$f(:$field) = 0,
Str :$d(:$delimiter) = ',',
Str :$t(:$template) = '%') {
for @in-files -> $file {
my $io = $file.IO;
for $io.lines -> $line {
my @fields = $line.split($d);
my $file = $template.subst('%',@fields[$f]);
$file ~= $io.extension;
if not %out_files{$file}:exists {
%out_files{$file} = $file.IO.open(:w);
}
%out_files{$file}.print($line);
}
}
%out_files>>.close;
}
@MattOates
Copy link
Author

===SORRY!===
Shape declaration with () is reserved;
please use whitespace if you meant a subsignature for unpacking,
or use the :() form if you meant to add signature info to the function's type
at /Users/matt/bin/explode:7
------> Int :$f⏏(:$field) = 0,
Shape declaration with () is reserved;
please use whitespace if you meant a subsignature for unpacking,
or use the :() form if you meant to add signature info to the function's type
at /Users/matt/bin/explode:8
------> Str :$d⏏(:$delimiter) = ',',
Shape declaration with () is reserved;
please use whitespace if you meant a subsignature for unpacking,
or use the :() form if you meant to add signature info to the function's type
at /Users/matt/bin/explode:9
------> Str :$t⏏(:$template) = '%') {
expecting any of:
shape declaration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment