Hi Gary,

please reply to all recipients.

On Thu, 3 Sep 2015 17:42:48 +0100
Gary Stainburn <gary.stainb...@ringways.co.uk> wrote:

> I have a hash of hashes which I converted to an array of hashes because I 
> needed to access them in a specific sequence.  It now looks like
> 
> my @pagetypes=(
>       {'pagetype'=>'Delivery  Note','weight'=>2,.....
>       {'pagetype'=>'Sales Invoice','weight'=>2,.....
>       {'pagetype'=>'Purchase Invoice','weight'=>2,.....
> ..........
> 
> I then access the array using
> 
> foreach my $pt (@pagetypes) {
> 
> 
> However, I have now lost the ability to access each pagetype by it's name
> 
> 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?
> 

You can maintain both an array and a hash that indexes it (or vice versa).
Something like this (untested pseudocode):

my @array;
my %index_to_array;

my $rec = { ... };
push @array, $rec;
$index_to_array{$rec->{'field'} } = $#array;

================

Hope it helps! If you found my advice helpful, and wish to help me in exchange,
then see http://www.shlomifish.org/meta/how-to-help/ .

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
http://www.shlomifish.org/humour/bits/Can-I-SCO-Now/ - “Can I SCO Now?”

No self‐respecting tomboy would use Mandriva.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to