Solved

remove whitespace from hash_ref

Posted on 2013-12-27
2
210 Views
Last Modified: 2013-12-27
I have hash is formatted like this
params => {test =>  ' 5 ' }

Open in new window


my goal is to strip any white space from the value of the test key.  What I have is not working of course.  How can I accomplish this in a 1 liner?  Thanks

$params->{$trim_filter->{$_}} = delete $params->{$_} foreach keys %$params;

Open in new window


#set the trim_filter
my $trim_filter = sub {
    return scalar($_[0] =~ s/^\s+|\s+$//g)
};

Open in new window

0
Comment
Question by:hexfusion
2 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39742869
#assuming you meant
$params = {test =>  ' 5 ' };

s/^\s+//,s/\s+$// for values %$params;
# or
$trim_filter->($_) for values %$params;


#if you meant
$params = {'  test   ' =>  5 };
#then you could use
$trim_filter = sub { $_[0] =~ s/^\s+|\s+$//g; return $_[0] }
#to do
$params->{$trim_filter->($_)} = delete $params->{$_} foreach keys %$params;
0
 
LVL 2

Author Closing Comment

by:hexfusion
ID: 39742989
Thanks as always ozo
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question