[11369] in Perl-Users-Digest
Perl-Users Digest, Issue: 4969 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Feb 26 11:27:31 1999
Date: Fri, 26 Feb 99 08:23:05 -0800
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 26 Feb 1999 Volume: 8 Number: 4969
Today's topics:
Re: FAQ 4.53: What happens if I add or remove keys from <kenhirsch@myself.com>
Re: FAQ 4.53: What happens if I add or remove keys from <zenin@bawdycaste.org>
Re: FAQ 4.53: What happens if I add or remove keys from (Bart Lateur)
Re: FAQ 4.53: What happens if I add or remove keys from <tchrist@mox.perl.com>
Re: FAQ 4.53: What happens if I add or remove keys from (Benjamin Franz)
Re: FAQ 4.53: What happens if I add or remove keys from <rick.delaney@home.com>
Re: FAQ 4.53: What happens if I add or remove keys from <ebohlman@netcom.com>
Re: FAQ 4.53: What happens if I add or remove keys from <tchrist@mox.perl.com>
Re: FAQ 4.53: What happens if I add or remove keys from (Abigail)
Re: FAQ 4.53: What happens if I add or remove keys from <tchrist@mox.perl.com>
Re: FAQ 4.53: What happens if I add or remove keys from (Abigail)
Re: FAQ 4.53: What happens if I add or remove keys from <tchrist@mox.perl.com>
Re: FAQ 4.53: What happens if I add or remove keys from (Sitaram Chamarty)
Re: FAQ 4.53: What happens if I add or remove keys from droby@copyright.com
Re: FAQ 4.53: What happens if I add or remove keys from droby@copyright.com
Re: FAQ 4.53: What happens if I add or remove keys from (Koos Pol)
Re: FAQ 4.53: What happens if I add or remove keys from <bmb@ginger.libs.uga.edu>
Re: FAQ 4.53: What happens if I add or remove keys from <tchrist@mox.perl.com>
FAQ 4.54: How do I look up a hash element by value? <perlfaq-suggestions@perl.com>
FAQ 4.55: How can I know how many entries are in a hash <perlfaq-suggestions@perl.com>
FAQ 4.56: How do I sort a hash (optionally by value ins <perlfaq-suggestions@perl.com>
FAQ 4.57: How can I always keep my hash sorted? <perlfaq-suggestions@perl.com>
FAQ 4.58: What's the difference between "delete" and "u <perlfaq-suggestions@perl.com>
FAQ 4.59: Why don't my tied hashes make the defined/exi <perlfaq-suggestions@perl.com>
FAQ 4.60: How do I reset an each() operation part-way t <perlfaq-suggestions@perl.com>
FAQ 4.61: How can I get the unique keys from two hashes <perlfaq-suggestions@perl.com>
Special: Digest Administrivia (Last modified: 12 Dec 98 (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 23 Feb 1999 13:07:41 -0500
From: "Ken Hirsch" <kenhirsch@myself.com>
Subject: Re: FAQ 4.53: What happens if I add or remove keys from a hash while iterating over it?
Message-Id: <7auqo0$a7m$1@fir.prod.itd.earthlink.net>
Benjamin Franz wrote in message ...
>In article <36d1e48d@csnews>,
>Tom Christiansen <perlfaq-suggestions@perl.com> wrote:
>>
>> What happens if I add or remove keys from a hash while iterating over
it?
>>
>> Don't do that.
>
>That's a fucking stupid answer. *IT DOESN'T ANSWER THE QUESTION*.
It is a perfectly good answer. The particular behavior will depend on
which key you delete, whether there are synonyms, the phase of the moon,
etc. You simply should not do it.
Also, swearing is really not necessary.
>
>"What happens if I export all my variables from my packages by default?"
>
> Don't do that.
>
>Or? Tom C. will flame you? Your mother-in-law won't love you? The Russian's
>will launch their total nuclear capability?
Tom is so nice to give these helpful answers and all you can do is complain!
>
>Second, just post the damn faq files. Chopping them into a billion
itty-bitty
>pieces that are then flooded to the group is annoying as hell.
Yes, but I think it makes it much more likely that people will actually read
each answer before posting their repetitive questions, which is even more
annoying.
------------------------------
Date: 23 Feb 1999 21:30:31 GMT
From: Zenin <zenin@bawdycaste.org>
Subject: Re: FAQ 4.53: What happens if I add or remove keys from a hash while iterating over it?
Message-Id: <919805526.390931@thrush.omix.com>
Tom Christiansen <tchrist@mox.perl.com> wrote:
: In comp.lang.perl.misc, Brad Baxter <bmb@ginger.libs.uga.edu> writes:
: :I'll add myself to those dozen and more thank-yous. It may be my
: :imagination, but there seem to be fewer FAQs posted now that the FAQs
: :are being posted.
:
: Thank you very much. And you're right -- the duplicate questions are
: going down, and I expect this to just get better. Now the group is
: filled with answers and discussion instead of the same tired questions.
: Isn't that great?
Yep!
Thanks Tom!
: --
: Do not meddle in the affairs of Wizards, for you are crunchy and
: go well with milk.
--
-Zenin (zenin@archive.rhps.org) From The Blue Camel we learn:
BSD: A psychoactive drug, popular in the 80s, probably developed at UC
Berkeley or thereabouts. Similar in many ways to the prescription-only
medication called "System V", but infinitely more useful. (Or, at least,
more fun.) The full chemical name is "Berkeley Standard Distribution".
------------------------------
Date: Tue, 23 Feb 1999 22:22:33 GMT
From: bart.lateur@skynet.be (Bart Lateur)
Subject: Re: FAQ 4.53: What happens if I add or remove keys from a hash while iterating over it?
Message-Id: <36d629db.1297328@news.skynet.be>
Zenin wrote:
>Tom Christiansen <tchrist@mox.perl.com> wrote:
>: Thank you very much. And you're right -- the duplicate questions are
>: going down, and I expect this to just get better. Now the group is
>: filled with answers and discussion instead of the same tired questions.
>: Isn't that great?
>
> Yep!
And it's a good time to brush off on the stuff you haven't looked at in
months.
Mind you, I'd probably think differently if the same FAQ's would get
reposted next week.
;-)
Bart.
------------------------------
Date: 23 Feb 1999 16:14:51 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: FAQ 4.53: What happens if I add or remove keys from a hash while iterating over it?
Message-Id: <36d3366b@csnews>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc, bart.lateur@skynet.be (Bart Lateur) writes:
:Mind you, I'd probably think differently if the same FAQ's would get
:reposted next week.
Can't happen. Takes twelve days and eight hours to cycle through
all of them. :-) (Barring stupid Linux sleep/EINTR bugs)
--tom
--
[End of diatribe. We now return you to your regularly scheduled
programming...]
--Larry Wall in Configure from the perl distribution
------------------------------
Date: Wed, 24 Feb 1999 00:24:54 GMT
From: snowhare@long-lake.nihongo.org (Benjamin Franz)
Subject: Re: FAQ 4.53: What happens if I add or remove keys from a hash while iterating over it?
Message-Id: <qFHA2.828$8N5.9804@typhoon-sf.pbi.net>
In article <7auqo0$a7m$1@fir.prod.itd.earthlink.net>,
Ken Hirsch <kenhirsch@myself.com> wrote:
>
>Benjamin Franz wrote in message ...
>
>It is a perfectly good answer. The particular behavior will depend on
>which key you delete, whether there are synonyms, the phase of the moon,
>etc. You simply should not do it.
*THATS* an answer. "The results are unpredictable."
>
>Also, swearing is really not necessary.
Probably not. But once in a blue moon it happens. If you do a DejaNews
search on my name you'll find perhaps enough incidents of me swearing
to count on the fingers of one hand in the last 5 years.
>>
>>"What happens if I export all my variables from my packages by default?"
>>
>> Don't do that.
>>
>>Or? Tom C. will flame you? Your mother-in-law won't love you? The Russian's
>>will launch their total nuclear capability?
>
>Tom is so nice to give these helpful answers and all you can do is complain!
That is actually the point. When Tom C. chooses to give good answers, he
gives excellent answers. When he chooses to be a smart ass, he is a
discredit to the entire Perl community. The comp.lang.perl.* crew in
general has acquired a rep as being intolerant of beginners and
a place where you better have a thick skin if you want to ask a
question. Tom has without a doubt done his part in creating this
reputation.
Tom also suffers excessively from "halo effect": Because hs is
a definitive source for Perl knowledge, people assume (wrongly)
that he is a definitive source for computer knowledge in general.
But the fact is that he is abyssmally weak in some very noticable areas.
For example, Pod::Html generates such incredibly poor HTML that
if Tom wrote Perl that way, it simply wouldn't even compile. It
is actually syntactically invalid.
This from the same person who has been known to flame
people for doing things with Perl that are only *questionable
practice*.
Perhaps I will take the time one of these years to do a ground up rewrite of
the POD system. The perlfaq system itself is obviously in severe need
of an overhaul and reorg - the fact is that unless you've already been
doing Perl for quite a while, you are unlikely to even know there
*IS* a perlfaq system in the Perl distribution. It isn't documented
in the Camel at all (interestingly it *does* assert that the place for
the FAQ to be posted is the *announce group. Not here.)
>>Second, just post the damn faq files. Chopping them into a billion
>itty-bitty
>>pieces that are then flooded to the group is annoying as hell.
>
>
>Yes, but I think it makes it much more likely that people will actually read
>each answer before posting their repetitive questions, which is even more
>annoying.
I disagree - what you have is a bi-weekly repeating flood of essentially
*random* snippits of the FAQs. It's like going to a library with the card
catalogs scattered on the floor. Yes, if you pick up a random card it
is likely to have something of interest to *someone* - but it is very
unlikely to be what you are looking for right now. It wastes the time
of many people for every one person lucky enough to pick up the exact
card they happened to need. People *occasionally* win the lottery.
Additionally, it look suspiciously spammish to me. I would not be terribly
surprised if one of the spam killers picked it up sooner or later.
Benjamin Franz
------------------------------
Date: Wed, 24 Feb 1999 01:44:00 GMT
From: Rick Delaney <rick.delaney@home.com>
Subject: Re: FAQ 4.53: What happens if I add or remove keys from a hash while iterating over it?
Message-Id: <36D35B36.5D86B5A@home.com>
[posted & mailed]
Tom Christiansen wrote:
>
> Can't happen. Takes twelve days and eight hours to cycle through
> all of them. :-)
Maybe the FAQs should be retitled, _Teach yourself Perl in 12 days and 8
hours_. Surely the numbers in the title would be more attractive than
the fact that they come free with perl. *sigh*
I'm even reading the ones I'm not interested in when they come spread
out like this.
Thanks a lot.
p.s.
What governs the order in which they're being presented?
--
Rick Delaney
rick.delaney@home.com
------------------------------
Date: Wed, 24 Feb 1999 02:29:18 GMT
From: Eric Bohlman <ebohlman@netcom.com>
Subject: Re: FAQ 4.53: What happens if I add or remove keys from a hash while iterating over it?
Message-Id: <ebohlmanF7n08u.4AC@netcom.com>
Bart Lateur <bart.lateur@skynet.be> wrote:
: Zenin wrote:
: >Tom Christiansen <tchrist@mox.perl.com> wrote:
: >: Thank you very much. And you're right -- the duplicate questions are
: >: going down, and I expect this to just get better. Now the group is
: >: filled with answers and discussion instead of the same tired questions.
: >: Isn't that great?
: >
: > Yep!
: And it's a good time to brush off on the stuff you haven't looked at in
: months.
Furthermore, the posting of the FAQ sections allows them to receive more
peer review. Misunderstandings, confusing wording, etc. get critiqued in
a forum where people are likely to point them out. The peer-reviewed
nature of Perl's documentation is one of its greatest strengths.
------------------------------
Date: 23 Feb 1999 20:02:46 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: FAQ 4.53: What happens if I add or remove keys from a hash while iterating over it?
Message-Id: <36d36bd6@csnews>
In comp.lang.perl.misc, snowhare@long-lake.nihongo.org (Benjamin Franz) writes:
:For example, Pod::Html generates such incredibly poor HTML that
:if Tom wrote Perl that way, it simply wouldn't even compile. It
:is actually syntactically invalid.
Nice try. The code was written by Chris Hall. See installhtml.
I've taken over the code so Chris doesn't get harrassed for bug fixes.
Thanks for being an asshole, even when you're wrong. It brightens
my day and cheers the whole newsgroup.
Oh, I forgot. You can't read this. Good riddance to bad rubbish.
--tom
--
An Inteligent terminal is not a smart-ass terminal; it is one you can educate.
--Rob Pike
------------------------------
Date: 24 Feb 1999 04:15:27 GMT
From: abigail@fnx.com (Abigail)
Subject: Re: FAQ 4.53: What happens if I add or remove keys from a hash while iterating over it?
Message-Id: <7avucv$csc$2@client2.news.psi.net>
Eric Bohlman (ebohlman@netcom.com) wrote on MMIII September MCMXCIII in
<URL:news:ebohlmanF7n08u.4AC@netcom.com>:
$$
$$ Furthermore, the posting of the FAQ sections allows them to receive more
$$ peer review. Misunderstandings, confusing wording, etc. get critiqued in
$$ a forum where people are likely to point them out. The peer-reviewed
$$ nature of Perl's documentation is one of its greatest strengths.
You mean, there are people who haven't killfiled them yet?
*boggle*
Abigail
--
perl -we '$_ = q ;4a75737420616e6f74686572205065726c204861636b65720as;;
for (s;s;s;s;s;s;s;s;s;s;s;s)
{s;(..)s?;qq qprint chr 0x$1 and \161 ssq;excess;}'
------------------------------
Date: 23 Feb 1999 21:30:39 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: FAQ 4.53: What happens if I add or remove keys from a hash while iterating over it?
Message-Id: <36d3806f@csnews>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc, abigail@fnx.com writes:
:You mean, there are people who haven't killfiled them yet?
:*boggle*
Hm.... if you have, how did you read this to respond? :-)
--tom
--
Unix is defined by whatever is running on Dennis Ritchie's machine.
------------------------------
Date: 24 Feb 1999 06:10:31 GMT
From: abigail@fnx.com (Abigail)
Subject: Re: FAQ 4.53: What happens if I add or remove keys from a hash while iterating over it?
Message-Id: <7b054n$diu$3@client2.news.psi.net>
Tom Christiansen (tchrist@mox.perl.com) wrote on MMIII September MCMXCIII
in <URL:news:36d3806f@csnews>:
() [courtesy cc of this posting sent to cited author via email]
()
() In comp.lang.perl.misc, abigail@fnx.com writes:
() :You mean, there are people who haven't killfiled them yet?
() :*boggle*
()
() Hm.... if you have, how did you read this to respond? :-)
%Score created by slrn on Mon Feb 22 11:22:01 1999
[comp.lang.perl.misc]
Score: -9999
%Expires:
% Subject: FAQ 4\.23: How do I reformat a paragraph\?
From: Tom Christiansen <perlfaq-suggestions@perl\.com>
% References: <36d04b0c@csnews>
% pubxfer\.news\.psi\.net comp\.lang\.perl\.misc:201261
% Newsgroup: comp\.lang\.perl\.misc
Abigail
--
%0=map{reverse+chop,$_}ABC,ACB,BAC,BCA,CAB,CBA;$_=shift().AC;1while+s/(\d+)((.)
(.))/($0=$1-1)?"$0$3$0{$2}1$2$0$0{$2}$4":"$3 => $4\n"/xeg;print#Towers of Hanoi
------------------------------
Date: 23 Feb 1999 23:14:48 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: FAQ 4.53: What happens if I add or remove keys from a hash while iterating over it?
Message-Id: <36d398d8@csnews>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc, abigail@fnx.com writes:
:%Score created by slrn on Mon Feb 22 11:22:01 1999
That's very cool. Scoring is loads better than blind killing/selecting.
I've got it in my newsreader, but confess to not having delved into it
due to suboptimal documentation. :-(
--tom
--
I already have too much problem with people thinking the efficiency of
a perl construct is related to its length. On the other hand, I'm
perfectly capable of changing my mind next week... :-) --lwall
------------------------------
Date: Thu, 25 Feb 1999 11:38:32 GMT
From: sitaram@diac.com (Sitaram Chamarty)
Subject: Re: FAQ 4.53: What happens if I add or remove keys from a hash while iterating over it?
Message-Id: <slrn7d92uo.t0.sitaram@sitagw2k.unidata.com>
On 23 Feb 1999 23:14:48 -0700, Tom Christiansen <tchrist@mox.perl.com> wrote:
> [courtesy cc of this posting sent to cited author via email]
>
>In comp.lang.perl.misc, abigail@fnx.com writes:
>:%Score created by slrn on Mon Feb 22 11:22:01 1999
>
>That's very cool. Scoring is loads better than blind killing/selecting.
>I've got it in my newsreader, but confess to not having delved into it
>due to suboptimal documentation. :-(
Holy cow! So there is one aspect of my newsreading that's even
better than Tom C's!
(Aside to Tom: you really ought to try slrn - it's a great
newsreader!)
(Sorry for being off-topic for the group, too!)
------------------------------
Date: Thu, 25 Feb 1999 12:52:29 GMT
From: droby@copyright.com
Subject: Re: FAQ 4.53: What happens if I add or remove keys from a hash while iterating over it?
Message-Id: <7b3h2a$n0h$1@nnrp1.dejanews.com>
In article <7auqo0$a7m$1@fir.prod.itd.earthlink.net>,
"Ken Hirsch" <kenhirsch@myself.com> wrote:
>
>
> Yes, but I think it makes it much more likely that people will actually read
> each answer before posting their repetitive questions, which is even more
> annoying.
>
Much more likely? Doubtful. But perhaps a little more likely. There seem
to be many people who post questions to this group requesting e-mail replies
who simply don't read the group or the faq at all. Loading the faq into
dejanews in small snippets may have value though, as some people may go to
that helpdesk before this one.
Tom's doing a Good Thing (tm) here, but I'll be surprised if it has much
effect.
--
Don Roby
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
------------------------------
Date: Thu, 25 Feb 1999 13:03:36 GMT
From: droby@copyright.com
Subject: Re: FAQ 4.53: What happens if I add or remove keys from a hash while iterating over it?
Message-Id: <7b3hn3$ng9$1@nnrp1.dejanews.com>
In article <36d31623@csnews>,
tchrist@mox.perl.com (Tom Christiansen) wrote:
> [courtesy cc of this posting sent to cited author via email]
>
> In comp.lang.perl.misc, Brad Baxter <bmb@ginger.libs.uga.edu> writes:
> :I'll add myself to those dozen and more thank-yous. It may be my
> :imagination, but there seem to be fewer FAQs posted now that the FAQs
> :are being posted.
>
> Thank you very much. And you're right -- the duplicate questions are
> going down, and I expect this to just get better. Now the group is
> filled with answers and discussion instead of the same tired questions.
> Isn't that great?
>
If it's true it's great. I hadn't noticed a decrease in faq posting. I
wonder if we can measure? I suppose maybe if we just track the new poster
count in the stats that might sort of reflect the volume of faqs, but there
are new posters with reasonable questions and there are people who repeatly
ask faqs...
But it's a good idea anyway.
Thank you.
--
Don Roby
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
------------------------------
Date: 26 Feb 1999 08:08:33 GMT
From: koos_pol@nl.compuware.com.NO_JUNK_MAIL (Koos Pol)
Subject: Re: FAQ 4.53: What happens if I add or remove keys from a hash while iterating over it?
Message-Id: <7b5kq2$sb0@news.nl.compuware.com>
On 23 Feb 1999 13:57:07 -0700, Tom Christiansen <tchrist@mox.perl.com> wrote:
| Thank you very much. And you're right -- the duplicate questions are
| going down, and I expect this to just get better. Now the group is
| filled with answers and discussion instead of the same tired questions.
| Isn't that great?
|
| --tom
The suggestion of a clp.faq is rather good imo. Then the knife cuts on
BOTH sides. What do you think?
Koos Pol
----------------------------------------------------------------------
S.C. Pol - Systems Administrator - Compuware Europe B.V. - Amsterdam
T:+31 20 3116122 F:+31 20 3116200 E:koos_pol@nl.compuware.com
Check my email address when you hit "Reply".
------------------------------
Date: Tue, 23 Feb 1999 15:40:29 -0500
From: Brad Baxter <bmb@ginger.libs.uga.edu>
Subject: Re: FAQ 4.53: What happens if I add or remove keys from a hash while iterating over it?
Message-Id: <Pine.A41.4.02.9902231536000.48018-100000@ginger.libs.uga.edu>
On 23 Feb 1999, Tom Christiansen wrote:
> Tough noogies. After the first dozen and a half legitimate and
> heart-felt thank-yous, I knew that this had been the right thing to do.
> Nobody reads the FAQs. They're too big. They are ignored.
> Just look at how many people are actually getting good use now.
> Doing it this way gives people a chance to look at things separately.
I'll add myself to those dozen and more thank-yous. It may be my
imagination, but there seem to be fewer FAQs posted now that the FAQs
are being posted.
-Brad
------------------------------
Date: 23 Feb 1999 13:57:07 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: FAQ 4.53: What happens if I add or remove keys from a hash while iterating over it?
Message-Id: <36d31623@csnews>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc, Brad Baxter <bmb@ginger.libs.uga.edu> writes:
:I'll add myself to those dozen and more thank-yous. It may be my
:imagination, but there seem to be fewer FAQs posted now that the FAQs
:are being posted.
Thank you very much. And you're right -- the duplicate questions are
going down, and I expect this to just get better. Now the group is
filled with answers and discussion instead of the same tired questions.
Isn't that great?
--tom
--
Do not meddle in the affairs of Wizards, for you are crunchy and
go well with milk.
------------------------------
Date: 22 Feb 1999 17:13:29 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 4.54: How do I look up a hash element by value?
Message-Id: <36d1f2a9@csnews>
(This excerpt from perlfaq4 - Data Manipulation
($Revision: 1.43 $, $Date: 1999/01/26 09:57:23 $)
part of the standard set of documentation included with every
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq4.html
if your negligent system adminstrator has been remiss in his duties.)
How do I look up a hash element by value?
Create a reverse hash:
%by_value = reverse %by_key;
$key = $by_value{$value};
That's not particularly efficient. It would be more space-efficient to
use:
while (($key, $value) = each %by_key) {
$by_value{$value} = $key;
}
If your hash could have repeated values, the methods above will only
find one of the associated keys. This may or may not worry you.
--
"Espousing the eponymous /cgi-bin/perl.exe?FMH.pl execution model is like
reading a suicide note -- three days too late."
--Tom Christiansen <tchrist@mox.perl.com>
------------------------------
Date: 22 Feb 1999 18:13:33 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 4.55: How can I know how many entries are in a hash?
Message-Id: <36d200bd@csnews>
(This excerpt from perlfaq4 - Data Manipulation
($Revision: 1.43 $, $Date: 1999/01/26 09:57:23 $)
part of the standard set of documentation included with every
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq4.html
if your negligent system adminstrator has been remiss in his duties.)
How can I know how many entries are in a hash?
If you mean how many keys, then all you have to do is take the scalar
sense of the keys() function:
$num_keys = scalar keys %hash;
In void context it just resets the iterator, which is faster for tied
hashes.
--
"If Dennis Ritchie were the man who developed Modula-2 then C would be long forgotten."
--Tarjei Jensen
------------------------------
Date: 22 Feb 1999 19:13:36 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 4.56: How do I sort a hash (optionally by value instead of key)?
Message-Id: <36d20ed0@csnews>
(This excerpt from perlfaq4 - Data Manipulation
($Revision: 1.43 $, $Date: 1999/01/26 09:57:23 $)
part of the standard set of documentation included with every
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq4.html
if your negligent system adminstrator has been remiss in his duties.)
How do I sort a hash (optionally by value instead of key)?
Internally, hashes are stored in a way that prevents you from imposing
an order on key-value pairs. Instead, you have to sort a list of the
keys or values:
@keys = sort keys %hash; # sorted by key
@keys = sort {
$hash{$a} cmp $hash{$b}
} keys %hash; # and by value
Here we'll do a reverse numeric sort by value, and if two keys are
identical, sort by length of key, and if that fails, by straight ASCII
comparison of the keys (well, possibly modified by your locale -- see
the perllocale manpage).
@keys = sort {
$hash{$b} <=> $hash{$a}
||
length($b) <=> length($a)
||
$a cmp $b
} keys %hash;
--
"A ship then new they built for him/of mithril and of elven glass"
--Larry Wall in perl.c from the v5.0 perl distribution,
citing Bilbo from Tolkien's LOTR
------------------------------
Date: 22 Feb 1999 20:13:39 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 4.57: How can I always keep my hash sorted?
Message-Id: <36d21ce3@csnews>
(This excerpt from perlfaq4 - Data Manipulation
($Revision: 1.43 $, $Date: 1999/01/26 09:57:23 $)
part of the standard set of documentation included with every
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq4.html
if your negligent system adminstrator has been remiss in his duties.)
How can I always keep my hash sorted?
You can look into using the DB_File module and tie() using the $DB_BTREE
hash bindings as documented in the section on "In Memory Databases" in
the DB_File manpage. The Tie::IxHash module from CPAN might also be
instructive.
--
Fine, let them give me the endless crap. --Andrew Hume
------------------------------
Date: 22 Feb 1999 21:13:42 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 4.58: What's the difference between "delete" and "undef" with hashes?
Message-Id: <36d22af6@csnews>
(This excerpt from perlfaq4 - Data Manipulation
($Revision: 1.43 $, $Date: 1999/01/26 09:57:23 $)
part of the standard set of documentation included with every
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq4.html
if your negligent system adminstrator has been remiss in his duties.)
What's the difference between "delete" and "undef" with hashes?
Hashes are pairs of scalars: the first is the key, the second is the
value. The key will be coerced to a string, although the value can be
any kind of scalar: string, number, or reference. If a key `$key' is
present in the array, `exists($key)' will return true. The value for a
given key can be `undef', in which case `$array{$key}' will be `undef'
while `$exists{$key}' will return true. This corresponds to (`$key',
`undef') being in the hash.
Pictures help... here's the `%ary' table:
keys values
+------+------+
| a | 3 |
| x | 7 |
| d | 0 |
| e | 2 |
+------+------+
And these conditions hold
$ary{'a'} is true
$ary{'d'} is false
defined $ary{'d'} is true
defined $ary{'a'} is true
exists $ary{'a'} is true (perl5 only)
grep ($_ eq 'a', keys %ary) is true
If you now say
undef $ary{'a'}
your table now reads:
keys values
+------+------+
| a | undef|
| x | 7 |
| d | 0 |
| e | 2 |
+------+------+
and these conditions now hold; changes in caps:
$ary{'a'} is FALSE
$ary{'d'} is false
defined $ary{'d'} is true
defined $ary{'a'} is FALSE
exists $ary{'a'} is true (perl5 only)
grep ($_ eq 'a', keys %ary) is true
Notice the last two: you have an undef value, but a defined key!
Now, consider this:
delete $ary{'a'}
your table now reads:
keys values
+------+------+
| x | 7 |
| d | 0 |
| e | 2 |
+------+------+
and these conditions now hold; changes in caps:
$ary{'a'} is false
$ary{'d'} is false
defined $ary{'d'} is true
defined $ary{'a'} is false
exists $ary{'a'} is FALSE (perl5 only)
grep ($_ eq 'a', keys %ary) is FALSE
See, the whole entry is gone!
--
"I woudn't recommend sex, drugs, or Unix for everyone, but they work for me."
Jim Thompson (jthomp@tadpole.com), paraphrasing Hunter S. Thompson
------------------------------
Date: 22 Feb 1999 22:13:44 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 4.59: Why don't my tied hashes make the defined/exists distinction?
Message-Id: <36d23908@csnews>
(This excerpt from perlfaq4 - Data Manipulation
($Revision: 1.43 $, $Date: 1999/01/26 09:57:23 $)
part of the standard set of documentation included with every
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq4.html
if your negligent system adminstrator has been remiss in his duties.)
Why don't my tied hashes make the defined/exists distinction?
They may or may not implement the EXISTS() and DEFINED() methods
differently. For example, there isn't the concept of undef with hashes
that are tied to DBM* files. This means the true/false tables above will
give different results when used on such a hash. It also means that
exists and defined do the same thing with a DBM* file, and what they end
up doing is not what they do with ordinary hashes.
--
As far as we know, our computer has never had an undetected error.
--Weisert
------------------------------
Date: 22 Feb 1999 23:13:45 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 4.60: How do I reset an each() operation part-way through?
Message-Id: <36d24719@csnews>
(This excerpt from perlfaq4 - Data Manipulation
($Revision: 1.43 $, $Date: 1999/01/26 09:57:23 $)
part of the standard set of documentation included with every
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq4.html
if your negligent system adminstrator has been remiss in his duties.)
How do I reset an each() operation part-way through?
Using `keys %hash' in scalar context returns the number of keys in the
hash *and* resets the iterator associated with the hash. You may need to
do this if you use `last' to exit a loop early so that when you re-enter
it, the hash iterator has been reset.
--
The Unix Way of doing something [...] is to make it look as much like a filter
as possible. (Richard O'Keefe)
------------------------------
Date: 23 Feb 1999 00:13:48 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 4.61: How can I get the unique keys from two hashes?
Message-Id: <36d2552c@csnews>
(This excerpt from perlfaq4 - Data Manipulation
($Revision: 1.43 $, $Date: 1999/01/26 09:57:23 $)
part of the standard set of documentation included with every
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq4.html
if your negligent system adminstrator has been remiss in his duties.)
How can I get the unique keys from two hashes?
First you extract the keys from the hashes into arrays, and then solve
the uniquifying the array problem described above. For example:
%seen = ();
for $element (keys(%foo), keys(%bar)) {
$seen{$element}++;
}
@uniq = keys %seen;
Or more succinctly:
@uniq = keys %{{%foo,%bar}};
Or if you really want to save space:
%seen = ();
while (defined ($key = each %foo)) {
$seen{$key}++;
}
while (defined ($key = each %bar)) {
$seen{$key}++;
}
@uniq = keys %seen;
--
An elephant is a mouse with an Microsoft operating system
------------------------------
Date: 12 Dec 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Special: Digest Administrivia (Last modified: 12 Dec 98)
Message-Id: <null>
Administrivia:
Well, after 6 months, here's the answer to the quiz: what do we do about
comp.lang.perl.moderated. Answer: nothing.
]From: Russ Allbery <rra@stanford.edu>
]Date: 21 Sep 1998 19:53:43 -0700
]Subject: comp.lang.perl.moderated available via e-mail
]
]It is possible to subscribe to comp.lang.perl.moderated as a mailing list.
]To do so, send mail to majordomo@eyrie.org with "subscribe clpm" in the
]body. Majordomo will then send you instructions on how to confirm your
]subscription. This is provided as a general service for those people who
]cannot receive the newsgroup for whatever reason or who just prefer to
]receive messages via e-mail.
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V8 Issue 4969
**************************************