http://www.perlmonks.org?node_id=1025457

Dr Manhattan has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

I have a list of words in a hash. I want to delete all the spaces at the end of each element. The element should only consist of characters, no spaces at all.

foreach my $words (keys %hash) { if ($words =~ /( )*$/) { delete $1; } }

Or will this delete the entire element? Any help would be appreciated

Replies are listed 'Best First'.
Re: Delete space at the end of a hash element
by 2teez (Vicar) on Mar 26, 2013 at 09:25 UTC

    Or using map function, with hdb data like so:

    use warnings; use strict; use Data::Dumper; my %hash = ( "apples " => 4, "oranges " => 5 ); %hash = map { my $val = $hash{$_}; s/\s+$//; $_, $val } keys %hash; print Dumper \%hash;

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

      So you are completely rebuilding the hash in one go, rather than adding/removing one entry at a time. That's far more elegant!

Re: Delete space at the end of a hash element
by kcott (Archbishop) on Mar 26, 2013 at 09:57 UTC

    G'day Dr Manhattan,

    Your requirements are ambiguous. Do you want to delete spaces from the end ("I want to delete all the spaces at the end of each element.") or do you want to delete all spaces ("The element should only consist of characters, no spaces at all.")? If the former, then substitution with s/// (as shown by hdb) is a good option; if the latter, then transliteration with y/// might be better.

    $ perl -Mstrict -Mwarnings -E ' my %original_hash = ( none => "abc", start => " abc", middle => "a b c", end => "abc ", both => " abc ", multi => " a b c ", ); say "Data with assorted spaces:"; say "$_\t|$original_hash{$_}|" for keys %original_hash; say "Data with no spaces at the end:"; my %no_spaces_at_end = %original_hash; $no_spaces_at_end{$_} =~ s/\s*$// for keys %no_spaces_at_end; say "$_\t|$no_spaces_at_end{$_}|" for keys %no_spaces_at_end; say "Data with no spaces at all:"; my %no_spaces_at_all = %original_hash; $no_spaces_at_all{$_} =~ y/ //d for keys %no_spaces_at_all; say "$_\t|$no_spaces_at_all{$_}|" for keys %no_spaces_at_all; ' Data with assorted spaces: middle |a b c| none |abc| both | abc | multi | a b c | end |abc | start | abc| Data with no spaces at the end: middle |a b c| none |abc| both | abc| multi | a b c| start | abc| end |abc| Data with no spaces at all: middle |abc| none |abc| both |abc| multi |abc| start |abc| end |abc|

    -- Ken

Re: Delete space at the end of a hash element
by SuicideJunkie (Vicar) on Mar 26, 2013 at 17:45 UTC

    This sounds a bit like an XY problem...
    The best way to clean up a mess is to not make one in the first place

    Why not simply trim the whitespace while you are first building the hash?

      I fully agree. Clean up the data before you store it in the hash, if you can.
Re: Delete space at the end of a hash element
by hdb (Monsignor) on Mar 26, 2013 at 08:21 UTC

    My understanding is that you want to remove spaces from the keys of a hash. I think you need to remove the entry from the hash and create a new one.

    my %hash = ( "apples " => 4, "oranges " => 5 ); foreach my $words (keys %hash) { my $elem = $hash{$words}; delete $hash{$words}; $words =~ s/\s*$//; $hash{$words} = $elem; } print %hash;

    Your proposal would not run but giving the error message:

    delete argument is not a HASH or ARRAY element or slice at hashdel.pl +line 5.

    This is because delete removes an entry from a hash but does not modify the key or the value.

Re: Delete space at the end of a hash element
by space_monk (Chaplain) on Mar 26, 2013 at 12:43 UTC

    Ye Olde Fashioned Waye.....

    my %newhash; while (my ($k, $v) = each( %hash)) { $k =~ s/\s+$//; $newhash{$k} = $v; } # newhash contains correct key values
    A Monk aims to give answers to those who have none, and to learn from those who know more.
Re: Delete space at the end of a hash element
by vinoth.ree (Monsignor) on Mar 26, 2013 at 11:06 UTC
    Update:

    Oops!!! I have given script to remove white spaces from values of hash...

    I want to delete all the spaces at the end of each element

    use strict; use warnings; use Data::Dumper; my %var_h =(1 => " value", "2" => "two "); s/(\s+)$//g for values %var_h; print Dumper \%var_h;

    The element should only consist of characters, no spaces at all.

    use strict; use warnings; use Data::Dumper; my %var_h =(1 => " value", "2" => "two "); s/\s+//g for values %var_h; print Dumper \%var_h;

    All is well