Solved

remove whitespace from hash_ref

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
git fat pull happens nothing in centos 6.3 python 2.6.6 33 217
collecting information 2 162
Move Function in Perl Script 2 68
Perl Awk Need Help 3 119
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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 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