
Kanji
User
Aug 12, 2000, 5:54 AM
Post #2 of 2
(114 views)
|
This was already addressed in this forum but a few days ago: hashes aren't ordered like you think, so if you want a specific order, you're responsible for making it happen. Anyhoo, to that end, you can create an array of keys in the order you want, and iterate over your results that way ... <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> @order = ( "Shipping Method", "Shipping Form", "..." ); foreach $key ( @order ) { print "VALUE: $key\n"; print "VALUE: $order_forms{ $key }\n"; }</pre><HR></BLOCKQUOTE> ... or use multi-dimensional constructs, like an array of hashes or an array of arrays (since arrays are always in the same order you put them in). <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> @order_forms = ( [ "Shipping Method" => "" ], [ "Shipping Form" => "On" ], [ "..." => "..." ], ); foreach $pair ( @order_forms ) { print "VALUE: $pair->[0]\n"; # ie, Shipping Form print "VALUE: $pair->[1]\n"; # ie, On }</pre><HR></BLOCKQUOTE> ... or make sure your keys are in sortable order ... <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> %order_forms = ( "a Shipping Method" => "", "b Shipping Form" => "On", "c ..." => "...", ); foreach $key ( sort keys %order_forms ) { ( my $name = $key ) =~ s/^. //; print "VALUE: $name\n"; print "VALUE: $order_forms{ $key }\n"; }</pre><HR></BLOCKQUOTE> [This message has been edited by Kanji (edited 08-12-2000).]
|