Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8040465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:54:17+00:00 2026-06-05T03:54:17+00:00

I basically want to do this: foreach my $key (keys $hash_ref) { Do stuff

  • 0

I basically want to do this:

foreach my $key (keys $hash_ref) {

    Do stuff with my $key and $hash_ref

    # Delete the key from the hash
    delete $hash_ref->{$key};
}

Is it safe? And why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:54:19+00:00Added an answer on June 5, 2026 at 3:54 am

    You’re not iterating over the hash, you’re iterating over the list of keys returned by keys before you even started looping. Keep in mind that

    for my $key (keys %$hash_ref) {
       ...
    }
    

    is roughly the same as

    my @anon = keys %$hash_ref;
    for my $key (@anon) {
       ...
    }
    

    Deleting from the hash causes no problem whatsoever.


    each, on the other, does iterate over a hash. Each time it’s called, each returns a different element. Yet, it’s still safe to delete the current element!

    # Also safe
    while (my ($key) = each(%$hash_ref)) {
       ...
       delete $hash_ref->{$key};
       ...
    }
    

    If you add or delete a hash’s elements while iterating over it, entries may be skipped or duplicated–so don’t do that. Exception: It is always safe to delete the item most recently returned by each()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I basically want to do this in code: PersonList myPersonList; //populate myPersonList here, not
Basically i want to do this. aa causes a bad cast exception. NOTE: o
This is what I'm talking about: http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ComboBox/ComboBox.aspx basically I want to have a drop
I've Googled for this but must be using the wrong keywords. Basically I want
I have this form. Basically what I want is to send a auto-response with
I just want to design this very simple website. Basically there are multiple pages
Basically i got this for loop and i want the number inputed (eg. 123)
Basically, I want to do something like this (in Python, or similar imperative languages):
Basically, I'm doing this: var phone = $(#phone).val(); Then I want to check if:
Ok, I need keys to be preserved within this array and I just want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.