Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Keeping order in a hash

2 views
Skip to first unread message

raan...@acc.jc.edu

unread,
Apr 27, 1999, 3:00:00 AM4/27/99
to
I am reading a file and creating a hash from the data read from that file.
The problem is that when the data is read in, instead of keeping the order
that the file is in, the hash is put in alphabetical order. My code is this:

while (<FILE>){
if (/^FAM: (.*)/) {
$family = $1;
}
if (/^PIC: (.*)/) {
@bflypics = split / /, $1;
foreach $picture (@bflypics) {
$picturegroups{$family}{$picture} = $common;
}
}

I need to have the %picturegroups hash sorted by $family but not in
alphabetical order, which it is automatically put in. The file from which
the data is read is already in the correct order but when put into the hash,
it is put in alphabetically. Is there a simple fix for this? Any help would
be greatly apprectiated! Thanks in advance!

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

Larry Rosler

unread,
Apr 27, 1999, 3:00:00 AM4/27/99
to
[Posted and a courtesy copy mailed.]

In article <7g58ea$g3d$1...@nnrp1.dejanews.com> on Tue, 27 Apr 1999
20:58:49 GMT, raan...@acc.jc.edu <raan...@acc.jc.edu> says...


> I am reading a file and creating a hash from the data read from that file.
> The problem is that when the data is read in, instead of keeping the order
> that the file is in, the hash is put in alphabetical order.

What makes you think that? Hashes are inherently unordered.

> My code is this:
>
> while (<FILE>){
> if (/^FAM: (.*)/) {
> $family = $1;
> }
> if (/^PIC: (.*)/) {
> @bflypics = split / /, $1;
> foreach $picture (@bflypics) {
> $picturegroups{$family}{$picture} = $common;
> }
> }
>
> I need to have the %picturegroups hash sorted by $family but not in
> alphabetical order, which it is automatically put in. The file from which
> the data is read is already in the correct order but when put into the hash,
> it is put in alphabetically.

You must be a fan of Lewis Carroll. From 'The Hunting of the Snark':

What I tell you three times is true.

No matter how many times you say this, it isn't so!

> Is there a simple fix for this? Any help would
> be greatly apprectiated! Thanks in advance!

RTFFAQ. perlfaq4: "How can I always keep my hash sorted?"

If the data from the file are already in the order you want, you might
use an array. Or you can sort on the keys of your hash.

--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
l...@hpl.hp.com

Abigail

unread,
Apr 28, 1999, 3:00:00 AM4/28/99
to
raan...@acc.jc.edu (raan...@acc.jc.edu) wrote on MMLXV September
MCMXCIII in <URL:news:7g58ea$g3d$1...@nnrp1.dejanews.com>:

"" I am reading a file and creating a hash from the data read from that file.
"" The problem is that when the data is read in, instead of keeping the order
"" that the file is in, the hash is put in alphabetical order.

Well, don't keep it in a hash then! Note that a hash doesn't put it
in alphabetical order. It puts it in some order, which sometimes is
alpabetical, but often it's not.

If you want to keep the same order as you read it from file, use an array.

Abigail
--
perl -we 'print split /(?=(.*))/s => "Just another Perl Hacker\n";'

0 new messages