On Thursday 03 September 2015 18:19:57 Marius Gavrilescu wrote:
> [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
> }
Hi Marius, I did think that something like this would work. I think this is
probably the route I'll take. I did like the hash of index numbers that
Shlomi suggested. The down side is that it's a two stage init
>
> 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
> }
This intrigues me as I can't see where you specify the sequence field. I'll go
Googling
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/