Skip to content

Instantly share code, notes, and snippets.

@ab5tract
Created January 8, 2025 17:01
Show Gist options
  • Save ab5tract/89c42a1ebd7c67a86353ce00e6dfceb6 to your computer and use it in GitHub Desktop.
Save ab5tract/89c42a1ebd7c67a86353ce00e6dfceb6 to your computer and use it in GitHub Desktop.
A small csv splitter that can do custom handling of values
# For a line that has two values, which may be missing but will always be in the order of Integer,String
class Line {
my @default-values = [ 42, Str ]; # purely theoretical
has @.values;
method new($line) {
my @values = $line.split(',');
for ^2 -> $idx {
@values[$idx] ||= @default-values[$idx];
}
self.bless(:@values);
}
}
my $parsed-line = Line.new(',"life"');
dd $parsed-line.values;
my $second = Line.new('555,""');
dd $second.values;
my $third = Line.new(',');
dd $third.values;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment