Browse files

added :skip-header handling

  • Loading branch information...
1 parent c15254f commit 1e15984c2b2f47357009ce1f9d6ed61ca941e5dc @masak committed Aug 21, 2009
Showing with 20 additions and 4 deletions.
  1. +8 −3 README
  2. +6 −1 lib/Text/CSV.pm
  3. +6 −0 t/04-output.t
View
11 README
@@ -8,10 +8,15 @@ Simple CSV (comma-separated values) format parser for Perl 6.
Oh, and the C<read> methods take this flag:
- :trim Removes whitespace on both ends of each value.
+ :trim Removes whitespace on both ends of each value.
- :output Determines the shape of the returned data structure.
- Allowed values are 'arrays' and 'hashes'.
+ :skip-header Causes the first line not to be included in the output.
+
+ :output Determines the shape of the returned data structure.
+ Allowed values are 'arrays' (the default) and 'hashes'.
+ When the value is 'hashes', the first line is assumed
+ to be a special header line, the values on that
+ line are used as hash keys, and :skip-header is suppressed.
== License
View
7 lib/Text/CSV.pm
@@ -16,7 +16,7 @@ class Text::CSV {
return $trim ?? $text.trim !! $text;
}
- method read($input, :$trim, :$output = 'arrays') {
+ method read($input, :$trim, :$output = 'arrays', :$skip-header) {
Text::CSV::File.parse($input)
or die "Sorry, cannot parse";
my @lines = $<line>;
@@ -30,6 +30,11 @@ class Text::CSV {
\%hash
}, @values;
}
+ elsif $output.lc eq 'arrays' {
+ if $skip-header {
+ @values.shift;
+ }
+ }
return @values;
}
View
6 t/04-output.t
@@ -22,6 +22,12 @@ is_deeply Text::CSV.read($input, :output<arrays>),
[<arthur extracts excalibur>] ],
'with :output<arrays>, an AoA is returned, header included';
+is_deeply Text::CSV.read($input, :skip-header),
+ [ [<dog bites man>],
+ [<child gets cake>],
+ [<arthur extracts excalibur>] ],
+ 'with :skip-header, the first line is left out';
+
is_deeply Text::CSV.read($input, :output<hashes>),
[ { :subject<dog>, :predicate<bites>, :object<man> },
{ :subject<child>, :predicate<gets>, :object<cake> },

0 comments on commit 1e15984

Please sign in to comment.