in reply to TIMTOWTDI doesn't mean invent an outlandish approach (usually)

Then the answers roll in. There are subtile differences, and so many of the answ ers are entirely valid approaches depending on how the context of the question i s intrepreted:
  • %hash = ();
  • undef %hash;
  • %hash = undef;
  • delete @hash{(keys %hash)};
But eventually things get a little over the top in the context of such a simple question:
  • map { delete $hash{$_} } keys %hash;
  • while (my ($k,$v) = each %hash) { delete $hash{$k} };
  • for (keys %hash) { delete $hash{$_} };
  • delete $hash{$_} for keys %hash;
I wouldn't call them "entirely valid". Of the first set of answers, one doesn't end up with an empty hash, and one is inefficient. Of the second set of answers, only one is correct, the other three are not guaranteed to work.
Of course, "The Perl Way" is that there is more than one way to do it. But do we really need to consider the possibility of using, my $len = do { split //, $string; my $counter; $counter++ foreach @_; $counter }; to determine the length of a string just because it's in the spirit of "Another Way To Do It"?
Oh, I've given answers like that. That is, I've given answers like that if the question is "how do I determine the length of a string". If someone doesn't have the decency to do even the most trivial research in the documentation, I'd consider such a person to be cannon fodder.

Now, if I were to get paid, I wouldn't have given such a answer, but replied with a question of the form "now, if you were going to look it up in the manual, which guess for a function name would you try first?". But here the questioners don't come with paychecks. The task of the people asking the questions is to keep it interesting for the people answering the questions. That can be done by asking interesting questions, refraining from asking FAQs and trivial questions, or to be at the receiving end of wit and sarcasm.

Abigail

  • Comment on Re: TIMTOWTDI doesn't mean invent an outlandish approach (usually)

Replies are listed 'Best First'.
OT: clearing a hash (was: TIMTOWTDI doesn't mean invent an outlandish approach (usually))
by Aristotle (Chancellor) on Oct 13, 2003 at 10:25 UTC
    Of the first set of answers, one doesn't end up with an empty hash, and one is inefficient. Of the second set of answers, only one is correct, the other three are not guaranteed to work.

    I must be missing something obvious, because I can't quite see why they wouldn't work. But since you say it applies for three of them, it could also be true for the inefficient answer from the first set.(Which I posted without actually looking at the question, in reply to someone posting one of the ones in the second set.) Is this so?

    I'm sure I'll bang my head on the wall once you spill the beans, but what is it that I can't see?

    Makeshifts last the longest.

      The '%hash = undef' is incorrect:
      $ perl -wle '%hash = undef; print scalar %hash' Odd number of elements in hash assignment at -e line 1. Use of uninitialized value in list assignment at -e line 1. 1/8
      It'll create a one element hash, whose key is the empty string, with an undefined value.

      The last three solutions modify the hash while iterating over the keys, which should be a no-no. However, I just read that in the documentation of 'each' that deleting the item most recently returned by 'each()' is safe; this feature seems to have been added (or documented) in 5.6.1.

      Abigail

        Ah, so that's why I was puzzled - the hint on each was there the first I wanted to know about deleting keys while iterating. But you said three of the following will not necessarily work:
        1. map { delete $hash{$_} } keys %hash;
        2. while (my ($k,$v) = each %hash) { delete $hash{$k} };
        3. for (keys %hash) { delete $hash{$_} };
        4. delete $hash{$_} for keys %hash;
        With the each solution being valid that's one down, two to go, and now I'm more confused than before.. of the remaining three solutions, the for loops are identical except for syntactical details. So they must either both be valid or both invalid; then the map version must be valid and the for ones not. But I can't see any reason to conclude that any of them (all four) are not guaranteed to work.

        Makeshifts last the longest.

Re: Re: TIMTOWTDI doesn't mean invent an outlandish approach (usually)
by welchavw (Pilgrim) on Oct 13, 2003 at 13:53 UTC
    or maybe canon fodder?