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


In reply to Re: TIMTOWTDI doesn't mean invent an outlandish approach (usually) by Abigail-II
in thread TIMTOWTDI doesn't mean invent an outlandish approach (usually) by davido

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.