/
fromsplit.pm
134 lines (106 loc) · 2.77 KB
/
fromsplit.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package App::RecordStream::Operation::fromsplit;
our $VERSION = "4.0.4";
use strict;
use base qw(App::RecordStream::Operation);
sub init {
my $this = shift;
my $args = shift;
my $headers = 0;
my $strict = 0;
my %options = (
"delim|d=s" => sub { $this->_set_delimiter($_[1]); },
"key|k|field|f=s" => sub { $this->add_field(split(/,/, $_[1])); },
"header" => \$headers,
"strict" => \$strict,
);
$this->parse_options($args, \%options);
$this->{'HEADER'} = $headers;
$this->{'STRICT'} = $strict;
}
sub _set_delimiter {
my ($this, $value) = @_;
$this->{'DELIMITER'} = $value;
}
sub get_delimiter {
my ($this) = @_;
if(!defined($this->{'DELIMITER'})) {
return ',';
}
return $this->{'DELIMITER'};
}
sub add_field {
my $this = shift;
$this->{'FIELDS'} ||= [];
push @{$this->{'FIELDS'}}, @_;
}
sub get_field {
my ($this, $index) = @_;
if($this->{'FIELDS'} && $index < @{$this->{'FIELDS'}}) {
return $this->{'FIELDS'}->[$index];
}
else {
return $index;
}
}
sub accept_line {
my $this = shift;
my $line = shift;
if ($this->{'HEADER'}) {
my $delim = $this->get_delimiter();
$this->add_field($_) for @{$this->get_values_for_line($line)};
delete $this->{'HEADER'};
}
else {
my $record = App::RecordStream::Record->new();
my $index = 0;
foreach my $value (@{$this->get_values_for_line($line)}) {
${$record->guess_key_from_spec($this->get_field($index))} = $value;
++$index;
}
$this->push_record($record);
}
return 1;
}
sub get_values_for_line {
my $this = shift;
my $line = shift;
my @values;
my $delim = $this->get_delimiter();
if ( $this->{'STRICT'} ) {
@values = split(/\Q$delim\E/, $line, -1);
}
else {
@values = split(/$delim/, $line, -1);
}
return \@values;
}
sub add_help_types {
my $this = shift;
$this->use_help_type('keyspecs');
}
sub usage {
my $this = shift;
my $options = [
[ 'delim|-d <delim>', "Delimiter to use for splitting input lines (default ',')."],
[ 'key|-k <key>', 'Comma separated list of key names. May be specified multiple times, may be key specs'],
[ 'header', 'Take key names from the first line of input.'],
[ 'strict', 'Delimiter is not treated as a regex'],
];
my $args_string = $this->options_string($options);
return <<USAGE;
Usage: recs-fromsplit <args> [<files>]
__FORMAT_TEXT__
Each line of input (or lines of <files>) is split on provided delimiter to
produce an output record. Keys are named numerically (0, 1, etc.) or as
given by --key.
__FORMAT_TEXT__
Arguments:
$args_string
Examples:
Parse space separated keys x and y.
recs-fromsplit --key x,y --delim ' '
Parse comma separated keys a, b, and c.
recs-fromsplit --key a,b,c
USAGE
}
1;