Skip to content

Instantly share code, notes, and snippets.

@er2
Last active December 19, 2020 20:05
Show Gist options
  • Save er2/b26c77e6a35e6f83472a6520da25bb38 to your computer and use it in GitHub Desktop.
Save er2/b26c77e6a35e6f83472a6520da25bb38 to your computer and use it in GitHub Desktop.
I saw this Expressive C++17 challenge going around and felt like solving it in Perl 6
sub MAIN($input, Str $column, Str $replacement, IO::Path $output="output.csv".IO) {
my Str $headings = $input.IO.lines[0];
my $index = $headings.split(",").antipairs.hash{$column};
die "column $column not found" unless $index ~~ Int:D;
my $fh = open $output, :w;
LEAVE close $fh;
for $input.IO.lines.skip: 1 -> $line {
my @arr = $line.split(",");
@arr[$index] = $replacement;
$fh.say(@arr.join(","));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment