http://www.perlmonks.org?node_id=1192550


in reply to Re: Can Text::CSV_XS return key-value pairs?
in thread Can Text::CSV_XS return key-value pairs?

Tux, can I add that to what I already have? Also, the value will not always be split. So can the value be first put through the filters then go to the on_in? And if this needs to use an additional hash, then how do I get everything to use the outside hash, the one you called %foo?

sub make_list { my (%opt) = @_; my $file = $opt{file} && ref($opt{file}) eq 'ARRAY' ? data_file(@{$o +pt{file}}) : $opt{file}; my $headers = $opt{'headings'} ? $opt{'headings'} : ['heading']; my $filters; for my $header (@$headers) { if ($header =~ s/\+$//) { $filters->{$header} = sub { $_ = [split(/;\s*/, $_)]; $_ }; } } my $list = csv ( in => $file, headers => $headers, key => $opt{key} ? $headers->[0] : undef, filter => $filters, sep_char => '|', quote_char => undef, empty_is_undef => 1, allow_whitespace => 1, auto_diag => 1, ); return $list; }

I did not want to include the code I am trying to replace with an all in one, but it looks like I have to.

sub make_hash { my %opt = @_; my $file = $opt{file} && ref($opt{file}) eq 'ARRAY' ? data_file(@{$o +pt{file}}) : $opt{file}; open(my $fh, '<', $file) || die "Can not open $file $!"; # If there are no headings, then 'heading' is the only heading. my @headings = $opt{headings} ? @{$opt{headings}} : ('heading'); my %hash; while (my $line = <$fh>) { chomp $line; my @values = split(/\|/,$line); # the key is always the first value, though if only one heading, t +hen only 1 value needed. my $key = scalar @headings > 1 ? $values[0] : shift @values; my $n = 0; for my $r_heading (@headings) { # skip any heading without a value if (defined($values[$n]) && length($values[$n]) > 0) { # split values if heading ends with a + my $split = $r_heading =~ /\+$/ ? 1 : 0; # but don't keep the + in the heading (my $heading = $r_heading) =~ s/\+$//; my $value = $split == 1 ? [map { $_ =~ s/^ //; $_ } split(/;/, +$values[$n])] : $values[$n]; # if more than 1 heading, make an HoH; only 1 heading, make H if (scalar @headings > 1) { $hash{$key}{$heading} = $value; } else { $hash{$key} = $value; } } $n++; } } return \%hash; } # This is simpler to convert, all I need to do is omit the key. # I didn't account for the possible splitting of the value because I h +adn't needed it. sub make_array { my %opt = @_; my $file = $opt{file} && ref($opt{file}) eq 'ARRAY' ? data_file(@{$o +pt{file}}) : $opt{file}; open(my $fh, '<', $file) || die "Can not open $file $!"; my @array; while (my $line = <$fh>) { chomp $line; my %hash; my @values = split(/\|/,$line); @hash{@{$opt{headings}}} = @values; push @array, \%hash; } return \@array; }
No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
Lady Aleena