Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added :skip-header handling

  • Loading branch information...
commit 1e15984c2b2f47357009ce1f9d6ed61ca941e5dc 1 parent c15254f
Carl Mäsak authored

Showing 3 changed files with 20 additions and 4 deletions. Show diff stats Hide diff stats

  1. +8 3 README
  2. +6 1 lib/Text/CSV.pm
  3. +6 0 t/04-output.t
11 README
@@ -8,10 +8,15 @@ Simple CSV (comma-separated values) format parser for Perl 6.
8 8
9 9 Oh, and the C<read> methods take this flag:
10 10
11   - :trim Removes whitespace on both ends of each value.
  11 + :trim Removes whitespace on both ends of each value.
12 12
13   - :output Determines the shape of the returned data structure.
14   - Allowed values are 'arrays' and 'hashes'.
  13 + :skip-header Causes the first line not to be included in the output.
  14 +
  15 + :output Determines the shape of the returned data structure.
  16 + Allowed values are 'arrays' (the default) and 'hashes'.
  17 + When the value is 'hashes', the first line is assumed
  18 + to be a special header line, the values on that
  19 + line are used as hash keys, and :skip-header is suppressed.
15 20
16 21 == License
17 22
7 lib/Text/CSV.pm
@@ -16,7 +16,7 @@ class Text::CSV {
16 16 return $trim ?? $text.trim !! $text;
17 17 }
18 18
19   - method read($input, :$trim, :$output = 'arrays') {
  19 + method read($input, :$trim, :$output = 'arrays', :$skip-header) {
20 20 Text::CSV::File.parse($input)
21 21 or die "Sorry, cannot parse";
22 22 my @lines = $<line>;
@@ -30,6 +30,11 @@ class Text::CSV {
30 30 \%hash
31 31 }, @values;
32 32 }
  33 + elsif $output.lc eq 'arrays' {
  34 + if $skip-header {
  35 + @values.shift;
  36 + }
  37 + }
33 38 return @values;
34 39 }
35 40
6 t/04-output.t
@@ -22,6 +22,12 @@ is_deeply Text::CSV.read($input, :output<arrays>),
22 22 [<arthur extracts excalibur>] ],
23 23 'with :output<arrays>, an AoA is returned, header included';
24 24
  25 +is_deeply Text::CSV.read($input, :skip-header),
  26 + [ [<dog bites man>],
  27 + [<child gets cake>],
  28 + [<arthur extracts excalibur>] ],
  29 + 'with :skip-header, the first line is left out';
  30 +
25 31 is_deeply Text::CSV.read($input, :output<hashes>),
26 32 [ { :subject<dog>, :predicate<bites>, :object<man> },
27 33 { :subject<child>, :predicate<gets>, :object<cake> },

0 comments on commit 1e15984

Please sign in to comment.
Something went wrong with that request. Please try again.