Created
January 8, 2025 17:01
-
-
Save ab5tract/89c42a1ebd7c67a86353ce00e6dfceb6 to your computer and use it in GitHub Desktop.
A small csv splitter that can do custom handling of values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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