The algo I would like to implement :
As I see it the steps are:
1. if the string has commas:
1.a. get the last comma and check if the last substring is a number - if so put it in hash like this: $hash{$path} = $value;
1.b. if the substring after the last comma isn't a number - $hash{$path} = 1;
2. if string has no commas: $hash{$string} = 1;
Although a good start, point 1.b. is unclear: in this case, do you want the whole string stored in $path, or just the part up until the last comma? For now I'm assuming the latter. Anyway, while there may always be "nicer" ways to write things in Perl (Update: and you haven't specified what you meant with "it doesn't look very good"), sometimes a good starting point is a direct translation:
use warnings;
use strict;
use Scalar::Util qw/looks_like_number/;
use Data::Dumper; # Debug
my %hash;
while (my $string = <DATA>) {
chomp($string);
# check if string has at least one comma, and at the same
# time extract the value after the last comma
if ( my ($path,$value) = $string=~/^(.*),([^,]*)$/ ) {
if ( looks_like_number($value) ) {
$hash{$path} = $value;
}
else {
$hash{$path} = 1;
}
}
else {
$hash{$string} = 1;
}
}
print Dumper(\%hash); # Debug
__DATA__
foo
bar,x
quz,5
a,b,c,42
Of course there's lots of potential for shortening that, e.g. by combining it with my example code from here. Update: A really simple shortening:
while (<DATA>) {
chomp;
if ( /^(.*),([^,]*)$/ )
{ $hash{$1} = looks_like_number($2) ? $2 : 1 }
else { $hash{$_} = 1 }
}