Gossamer Forum
Home : General : Perl Programming :

HEEEELP!!!!!!!! Hash field listing...

Quote Reply
HEEEELP!!!!!!!! Hash field listing...
Hey Guys,

I am having trouble going through a list of hashes, in order...

i.e. i have these hashes (i think thats what it's called... i get confused you know, hashes arrays, etc.. why didnt they call them bananas and apples?! Smile ):

$option{z} = "z";
$optionvalue{z} = "valuez";
$option{a} = "a";
$optionvalue{a} = "valuea";

then i have a loop that i want to go through each option{xxx}... but it is sorting it in some mestirous way.. (i dont think it is alfabetical!)

foreach $name (keys %option){
print $optionvalue{$name};
}

can anyone shed a light???

Thanks!

------------------


Sincerely,
Roy Nasser
roy@nasser.com.br
Quote Reply
Re: HEEEELP!!!!!!!! Hash field listing... In reply to
Hashes do not store in alphabetical order. We have the order that we define them in, but perl doesn't look at them like that.

what you could do is, place the keys into an array, sort the array, then go through and print out the value for each key in the array.

--mark
Quote Reply
Re: HEEEELP!!!!!!!! Hash field listing... In reply to
I don't want the sorted!!!

I want them in the order defined... is that possible??

Can you give me a saple code if so???

Thanks!

------------------


Sincerely,
Roy Nasser
roy@nasser.com.br
Quote Reply
Re: HEEEELP!!!!!!!! Hash field listing... In reply to
Hashes have no order.

--mark
Quote Reply
Re: HEEEELP!!!!!!!! Hash field listing... In reply to
Could you help me, what code would i use to process the hash above, in the order it was entered?

Thnks

------------------


Sincerely,
Roy Nasser
roy@nasser.com.br
Quote Reply
Re: HEEEELP!!!!!!!! Hash field listing... In reply to
Why the need for two hashes? You seem to just have a name = value combo.

Just do:

$option{'z'} = 'valuez';
$option{'a'} = 'valuea';

then:

foreach $name (keys %option) {
print "$name => $option{$name}\n";
}

to get the name,value back out.

Hope this helps,

Alex
Quote Reply
Re: HEEEELP!!!!!!!! Hash field listing... In reply to
I have more than one, baceuse it is not only value and name.. i also have $optiontype{x} = y;, etc...

the code you gave me also spits it out at a random order....

Weird...

I know that Matt's formmail uses a hash, and it spits it out in the same order as they are entered... i looked at the source but didn;t understand...



------------------


Sincerely,
Roy Nasser
roy@nasser.com.br
Quote Reply
Re: HEEEELP!!!!!!!! Hash field listing... In reply to
Matt's Formmail script defines an array called @Field_Order to define a generic order of fields. It is based on the order that the name-value pairs come from the query string.


------------------
JPD





Quote Reply
Re: HEEEELP!!!!!!!! Hash field listing... In reply to
I got to that point, but i coudn't apply it in my script...

Thanks anyways...



------------------


Sincerely,
Roy Nasser
roy@nasser.com.br
Quote Reply
Re: HEEEELP!!!!!!!! Hash field listing... In reply to
You can use something similar in your script. If you want them to be in the order you listed:

$option{z} = "z";
$optionvalue{z} = "valuez";
$option{a} = "a";
$optionvalue{a} = "valuea";

just add an array assignment:

$option{z} = "z";
$optionvalue{z} = "valuez";
$optionorder[0] = "z";
$option{a} = "a";
$optionvalue{a} = "valuea";
$optionorder[1] = "a";

Then, when you want to print them out, use:

Code:
foreach $optionkey (@optionorder) {
print $option{$optionkey};
print $optionvalue{$optionkey};
}



------------------
JPD





Quote Reply
Re: HEEEELP!!!!!!!! Hash field listing... In reply to
Well, when you insert things into a hash, the insert order is not preserved. If you want things returned alphabetically by key:

foreach $name ( sort keys %option ) {
print "$name => $option{$name}\n";
}

if you want things returned alphabetically by value:

foreach $name ( sort { $option{$a} cmp $option{$b} } keys %option ) {
print "$name => $option{$name}\n";
}

if you want things returned by the order you inserted them, then you'll need to keep an array index, and you might not even want to use a hash in the first place.

Hope this helps,

Alex