view release on metacpan or search on metacpan
lib/App/CSVUtils/csv_lookup_fields.pm view on Meta::CPAN
for my $row (@{ $r->{target_data_rows} }) {
my $key = join "|", map {
my $field = $r->{lookup_fields}[$_][0];
my $field_idx = $r->{target_fields_idx}{$field};
my $val = defined $field_idx ? $row->[$field_idx] : "";
$val = lc $val if $ci;
$val;
} 0..$#{ $r->{lookup_fields} };
#say "D:looking up '$key' ...";
if (defined(my $row_idx = $lookup_table{$key})) {
#say " D:found";
my $row_filled;
my $source_row = $r->{source_data_rows}[$row_idx];
for my $field (keys %{$r->{fill_fields}}) {
my $target_field_idx = $r->{target_fields_idx}{$field};
next unless defined $target_field_idx;
my $source_field_idx = $r->{source_fields_idx}{ $r->{fill_fields}{$field} };
next unless defined $source_field_idx;
$row->[$target_field_idx] =
$source_row->[$source_field_idx];
$row_filled++;
lib/App/CSVUtils/csv_pick_cells.pm view on Meta::CPAN
# we add this key to the stash
$r->{picked_cells} = [];
$r->{num_cells} = 0;
$r->{output_fields} = ['value'];
},
on_input_data_row => sub {
my $r = shift;
#say "D:input_data_rownum=$r->{input_data_rownum}";
if ($r->{util_args}{num_cells} == 1) {
for my $i (0 .. $#{ $r->{input_fields} }) {
# algorithm from Learning Perl
$r->{picked_cells}[0] = $r->{input_row}[$i]
if rand(++$r->{num_cells}) < 1;
}
} else {
for my $i (0 .. $#{ $r->{input_fields} }) {
$r->{num_cells}++;
# algorithm from Learning Perl, modified
lib/App/CSVUtils/csv_pick_rows.pm view on Meta::CPAN
$r->{picked_rows} = [];
# because input_* will be cleared by the time of after_read_input,
# we save and set it now
$r->{output_fields} = $r->{input_fields};
},
on_input_data_row => sub {
my $r = shift;
#say "D:input_data_rownum=$r->{input_data_rownum}";
if ($r->{util_args}{num_rows} == 1) {
# algorithm from Learning Perl
$r->{picked_rows}[0] = $r->{input_row} if rand($r->{input_data_rownum}) < 1;
} else {
# algorithm from Learning Perl, modified
if (@{ $r->{picked_rows} } < $r->{util_args}{num_rows}) {
# we haven't reached $num_rows, put row to result in a random
# position
splice @{ $r->{picked_rows} }, rand(@{ $r->{picked_rows} }+1), 0, $r->{input_row};
} else {
lib/App/CSVUtils/csv_setop.pm view on Meta::CPAN
my $code_get_compare_key = sub {
my ($file_idx, $row_idx) = @_;
my $row = $r->{all_input_data_rows}[$file_idx][$row_idx];
my $key = join "|", map {
my $field = $compare_fields[$_][$file_idx];
my $field_idx = $r->{all_input_fields_idx}[$file_idx]{$field};
my $val = defined $field_idx ? $row->[$field_idx] : "";
$val = uc $val if $ci;
$val;
} 0..$#compare_fields;
#say "D:compare_key($file_idx, $row_idx)=<$key>";
$key;
};
my $code_print_result_row = sub {
my ($file_idx, $row) = @_;
my @res_row = map {
my $field = $result_fields[$_];
my $field_idx = $r->{all_input_fields_idx}[$file_idx]{$field};
defined $field_idx ? $row->[$field_idx] : "";
} 0..$#result_fields;
lib/App/CSVUtils/csv_sort_fields_by_spec.pm view on Meta::CPAN
For example, modifying from example in `Sort::BySpec`'s Synopsis, you want to
sort these fields:
field1..field15 field42
as follow: 1) put fields with odd numbers first in ascending order; 2) put the
specific fields field4, field2, field42 in that order; 3) put the remaining
fields of even numbers in descending order. To do this:
% perl -E'say join(",",map {"field$_"} 1..15,42)' | csv-sort-fields-by-spec - \
'qr([13579]\z)' 'sub { my($a,$b)=@_; for($a,$b){s/field//} $a<=>$b }' \
field4 field2 field42 \
'sub { my($a,$b)=@_; for($a,$b){s/field//} $b<=>$a }'
The result:
field1,field3,field5,field7,field9,field11,field13,field15,field4,field2,field42,field14,field12,field10,field8,field6
MARKDOWN
remove_args => ['by_examples', 'by_code', 'by_sortsub'],
lib/App/CSVUtils/csv_sort_fields_by_spec.pm view on Meta::CPAN
For example, modifying from example in C<Sort::BySpec>'s Synopsis, you want to
sort these fields:
field1..field15 field42
as follow: 1) put fields with odd numbers first in ascending order; 2) put the
specific fields field4, field2, field42 in that order; 3) put the remaining
fields of even numbers in descending order. To do this:
% perl -E'say join(",",map {"field$_"} 1..15,42)' | csv-sort-fields-by-spec - \
'qr([13579]\z)' 'sub { my($a,$b)=@_; for($a,$b){s/field//} $a<=>$b }' \
field4 field2 field42 \
'sub { my($a,$b)=@_; for($a,$b){s/field//} $b<=>$a }'
The result:
field1,field3,field5,field7,field9,field11,field13,field15,field4,field2,field42,field14,field12,field10,field8,field6
This function is not exported.
lib/App/CSVUtils/csv_sort_rows.pm view on Meta::CPAN
name,age
Dennis,15
Andy,20
Jerry,30
Ben,30
If you use `--hash`, your code will receive the rows to be compared as hashref,
e.g. `--hash --by-code '$a->{age} <=> $b->{age} || $b->{name} cmp $a->{name}'.
A third alternative is to sort using <pm:Sort::Sub> routines. Example output
(using `--by-sortsub 'by_length<r>' --key '$_->[0]'`, which is to say to sort by
descending length of name):
name,age
Dennis,15
Jerry,30
Andy,20
Ben,30
If none of the `--by-*` options are specified, the utility will bail unless
there's a default that can be used, e.g. when CSV has a single field then that
lib/App/CSVUtils/csv_sort_rows.pm view on Meta::CPAN
name,age
Dennis,15
Andy,20
Jerry,30
Ben,30
If you use C<--hash>, your code will receive the rows to be compared as hashref,
e.g. `--hash --by-code '$a->{age} <=> $b->{age} || $b->{name} cmp $a->{name}'.
A third alternative is to sort using L<Sort::Sub> routines. Example output
(using C<< --by-sortsub 'by_lengthE<lt>rE<gt>' --key '$_-E<gt>[0]' >>, which is to say to sort by
descending length of name):
name,age
Dennis,15
Jerry,30
Andy,20
Ben,30
If none of the C<--by-*> options are specified, the utility will bail unless
there's a default that can be used, e.g. when CSV has a single field then that
lib/App/CSVUtils/paras2csv.pm view on Meta::CPAN
my $field = _unescape_field($1);
my $value = _unescape_value($2);
($field, $value);
}
sub _parse_para {
my ($r, $para, $idx) = @_;
my @h;
while ($para =~ s/\A(.+(?:\R .*)*)(?:\R|\z)//g) {
#say "D:line=<$1>, para=<$para>";
my ($field, $val) = _parse_line($1);
defined $field or die [400, "Paragraph[$idx]: Can't parse line $1"];
if ($r->{util_args}{trim_header}) {
$field =~ s/\A\s+//;
$field =~ s/\s+\z//;
} elsif ($r->{util_args}{ltrim_header}) {
$field =~ s/\A\s+//;
} elsif ($r->{util_args}{rtrim_header}) {
$field =~ s/\s+\z//;
}
lib/App/CSVUtils/paras2csv.pm view on Meta::CPAN
$fh = \*STDIN;
} else {
open $fh, "<", $r->{util_args}{input_file}
or die [500, "Can't read file '$r->{util_args}{input_file}: $!"];
}
local $/ = "";
my $i = 0;
while (my $para = <$fh>) {
$para =~ s/\R{2}\z//;
#say "D:para=<$para>";
my @h = _parse_para($r, $para, $i);
$i++;
if ($i == 1) {
my @h2 = @h;
my $j = 0;
while (my ($field, $value) = splice @h2, 0, 2) {
$r->{output_fields}[$j] = $field;
$r->{output_fields_idx}{$field} = $j;
$j++;
}
script/csv-sort view on Meta::CPAN
name,age
Dennis,15
Andy,20
Jerry,30
Ben,30
If you use C<--hash>, your code will receive the rows to be compared as hashref,
e.g. `--hash --by-code '$a->{age} <=> $b->{age} || $b->{name} cmp $a->{name}'.
A third alternative is to sort using L<Sort::Sub> routines. Example output
(using C<< --by-sortsub 'by_lengthE<lt>rE<gt>' --key '$_-E<gt>[0]' >>, which is to say to sort by
descending length of name):
name,age
Dennis,15
Jerry,30
Andy,20
Ben,30
If none of the C<--by-*> options are specified, the utility will bail unless
there's a default that can be used, e.g. when CSV has a single field then that
script/csv-sort-fields-by-spec view on Meta::CPAN
For example, modifying from example in C<Sort::BySpec>'s Synopsis, you want to
sort these fields:
field1..field15 field42
as follow: 1) put fields with odd numbers first in ascending order; 2) put the
specific fields field4, field2, field42 in that order; 3) put the remaining
fields of even numbers in descending order. To do this:
% perl -E'say join(",",map {"field$_"} 1..15,42)' | csv-sort-fields-by-spec - \
'qr([13579]\z)' 'sub { my($a,$b)=@_; for($a,$b){s/field//} $a<=>$b }' \
field4 field2 field42 \
'sub { my($a,$b)=@_; for($a,$b){s/field//} $b<=>$a }'
The result:
field1,field3,field5,field7,field9,field11,field13,field15,field4,field2,field42,field14,field12,field10,field8,field6
=head1 OPTIONS
script/csv-sort-rows view on Meta::CPAN
name,age
Dennis,15
Andy,20
Jerry,30
Ben,30
If you use C<--hash>, your code will receive the rows to be compared as hashref,
e.g. `--hash --by-code '$a->{age} <=> $b->{age} || $b->{name} cmp $a->{name}'.
A third alternative is to sort using L<Sort::Sub> routines. Example output
(using C<< --by-sortsub 'by_lengthE<lt>rE<gt>' --key '$_-E<gt>[0]' >>, which is to say to sort by
descending length of name):
name,age
Dennis,15
Jerry,30
Andy,20
Ben,30
If none of the C<--by-*> options are specified, the utility will bail unless
there's a default that can be used, e.g. when CSV has a single field then that