Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Remove key/value pairs from a hash

by cajun (Chaplain)
on Jun 11, 2005 at 05:24 UTC ( [id://465749]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to remove key/value pairs from a hash. According to Q&A How do I completely remove a key/value pair from my hash?, the following code I believe should work, but it isn't. Apparently, I'm missing something here.

%hash1=("key1"=>"value1","key2"=>"value2", "key3"=>"value3", "key4"=>" +value4", "key5"=>"value5"); @array=(key3, key4); foreach (@array){ delete($hash1{"$_"}); }

Thanks,
Mike

Replies are listed 'Best First'.
Re: Remove key/value pairs from a hash
by ikegami (Patriarch) on Jun 11, 2005 at 05:36 UTC

    Your code *does* work. What makes you think it doesn't?

    <your code here> require Data::Dumper; print(Data::Dumper::Dumper(\%hash1)); __END__ output ====== $VAR1 = { 'key5' => 'value5', 'key2' => 'value2', 'key1' => 'value1' };

    Of course, you should quote key3 and key4 (it's good practice, and it's strict-friendly):
    @array = ("key3", "key4");
    or
    @array = qw( key3 key4 );

    And why use
    "$_"
    when you can simply use
    $_

Re: Remove key/value pairs from a hash
by Zaxo (Archbishop) on Jun 11, 2005 at 05:43 UTC

    What doesn't work about it? If I add print keys %hash1; to the end of your code, it prints key5key2key1 What are you expecting?

    After Compline,
    Zaxo

Re: Remove key/value pairs from a hash
by dReKurCe (Scribe) on Jun 11, 2005 at 12:44 UTC
    Greetings: The following code worked for me:
    #! /usr/bin/perl %hash1=("key1"=>"value1","key2"=>"value2", "key3"=>"value3", "key4"=>" value4", "key5"=>"value5"); @array=(key3, key4); foreach (@array){ delete($hash1{"$_"}); } while (($key,$value)= each (%hash1)){ print "$key =$value\n"};
    by producing
    key5 =value5 key2 =value2 key1 =value1
Re: Remove key/value pairs from a hash
by cajun (Chaplain) on Jun 11, 2005 at 21:54 UTC
    Problem solved. It was so simple I'm embarrased to explain what happened. Yet perhaps someone who is searching for answers in the future, may stumble across this node and get some clues to their problem.

    The short answer to the problem was to look at everything. Not just the piece of code you think isn't working.

    Both %hash1 and @array are read in from a file. See where this is headed? I did a chomp when I was reading in the data for %hash1, but forgot to do a chomp when I was reading in the data for @array.

    %key3\n ne %key3 %key4\n ne %key4

    Thanks to all for the suggestions.
    Mike

Re: Remove key/value pairs from a hash
by cajun (Chaplain) on Jun 11, 2005 at 05:52 UTC
    Thanks ikegami & Zaxo.

    The reason I'm thinking it isn't working is if I print out %hash1 AFTER deleting the key/value pairs, it is the same size as it was before deleting the pairs.

    I must have an issue elsewhere. Thanks for the help. I'll continue looking.
    Mike

      I'm curious... What do you mean by the size of the hash? How do you obtain it?
Re: Remove key/value pairs from a hash
by cajun (Chaplain) on Jun 11, 2005 at 06:01 UTC
    Sorry, poor choice of words. The hash is written out to a file after the deletion of the records (key/value pairs). It has the same number of records (key/value pairs) after the deletion as it did before the deletion.

    Thanks,
    Mike

    EDIT -

    $before = keys %hash1; $after = keys %hash1;

      It still works... $before is 5, $after is 3

      The problem is obviously not with delete, but with your data. Why don't you dump the array, the before-hash and the after-hash using Data::Dumper as shown in my first post. That will show the problem with your real data.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://465749]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 01:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found