[email protected] (Gary Stainburn) writes:
> I am aware that you cannot define a key sequence when defining a hash.
> However, if I create a sequence field
>
> my %pagetypes=(
> 'Delivery Note'=>{'seq'=>1,weight'=>2,.....
> 'Sales Invoice'=>{'seq'=>2,'weight'=>2,.....
> 'Purchase Invoice'=>{'seq'=>3,'weight'=>2,.....
> ..........
>
> How can I do the foreach statement to access them in the correct sequence?
Use sort.
for my $key (sort { $pagetypes{$a}{seq} <=> $pagetypes{$b}{seq} } keys
%pagetypes) {
# do stuff with $pagetypes{key} here
}
The above can be shortened using the Sort::Key module.
use Sort::Key 'nkeysort';
for my $key (nkeysort { $pagetypes{$_} } keys %pagetypes) {
# do stuff with $pagetypes{key} here
}
--
Marius Gavrilescu
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/