Solved

remove whitespace from hash_ref

Posted on 2013-12-27
2
206 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now