in reply to Re: Unable to extract value from hash table
in thread Unable to extract value from hash table
Perhaps a slightly-safer split-regex might be: /\s*,\s*/ or something along those lines.
This splits on “a comma, preceded and followed by zero-or-more whitespace characters.” So the match will occur whether-or-not the comma is surrounded on either side by whitespace, and will “eat” both the comma and the whitespace.
but also note ...$ perl -e 'use Data::Dumper; my $a = "a, b , c,d"; my @b = split(/\s*, +\s*/, $a); print Data::Dumper->Dump([\@b], ['b']);' $b = [ 'a', 'b', 'c', 'd' ];
(Which is a valid and correct parse since the example string contains leading and trailing spaces, which split of course does not care about.)$ perl -e 'use Data::Dumper; my $a = " a, b , c,d "; my @b = split(/\s +*,\s*/, $a); print Data::Dumper->Dump([\@b], ['b']);' $b = [ ' a', 'b', 'c', 'd ' ];
Therefore, an equally good possible strategy therefore would be to split on /,/ alone, then separately remove leading and/or trailing whitespace from each piece. Or, remove any leading and/or trailing whitespace from the entire line before splitting it up as described. If you do not care to install String::Util to get access to a trim() function, you can also use this Perl-foo to remove leading and trailing whitespace: $str =~ s/^\s+|\s+$//g