If 'strip spaces' simply means to remove all spaces, here's one approach:
>perl -wMstrict -le "use Data::Dumper; my %hash = ( ' k 1 ' => ' v 1 ', ' k 2 ' => ' v 2 ', ); print Dumper \%hash; %hash = map { s{\s+}{}xmsg; $_ } %hash; print Dumper \%hash; " $VAR1 = { ' k 2 ' => ' v 2 ', ' k 1 ' => ' v 1 ' }; $VAR1 = { 'k2' => 'v2', 'k1' => 'v1' };
Beware, however, that if the original hash has a key ' k e y ' and also another key 'key', something is going to get clobbered.
Update: Per ikegami's reply Re^2: Strip spaces in hash, I would add the cautionary note that, had the output of map not ultimately been assigned to the source hash in the code above, the values of that hash would, as a side effect, and perhaps quite unexpectedly and mysteriously, have been altered. Many happy hours of debugging ensue.
In reply to Re: Strip spaces in hash
by AnomalousMonk
in thread Strip spaces in hash
by kalyanrajsista
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |