Perl: the Markov chain saw | |
PerlMonks |
Re^2: difference in regexby ovedpo15 (Pilgrim) |
on May 29, 2018 at 13:58 UTC ( [id://1215372]=note: print w/replies, xml ) | Need Help?? |
Thank you for the fast replay. I tried to use the following regex my($path,$value) = ($row =~ /(.*),(.*)/); to split the string. but if there are no commas it won't work. Which regex should I use in order to always put the string into $path so I can only check if $value is defined? for example: if "abc" it will be $path = "abc" and $value is undefined. if "abc,5" it will be $path = "abc" and $value = 5 if "a,b,c,5" it will be $path = "a,b,c" and $value = 5 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; how to implement this?
In Section
Seekers of Perl Wisdom
|
|