sub make_hash { my %opt = @_; my $file = $opt{file} && ref($opt{file}) eq 'ARRAY' ? data_file(@{$opt{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, then 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 hadn't needed it. sub make_array { my %opt = @_; my $file = $opt{file} && ref($opt{file}) eq 'ARRAY' ? data_file(@{$opt{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; }