Q: associative arrays 
Author Message
 Q: associative arrays

Hi,

another newbie question: Is there a way to force Perl to iterate over
the elements in an associative array in the order as it was given in
the array definition?
Example:

%dum = ( '3', '3', '1', '1', '2', '2' );

while (($key, $val) = each(%dum)) {
   print "$key/$val\n";

Quote:
}

This prints (not surprisingly):
1/1
2/2
3/3

but I want to see;

3/3
1/1
2/2

You may ask why I do not use normal arrays but I have good reasons not
to do so ...

Cheers



Tue, 11 Jan 2000 03:00:00 GMT  
 Q: associative arrays

Quote:

> another newbie question: Is there a way to force Perl to iterate over
> the elements in an associative array in the order as it was given in
> the array definition?

    http://www.perl.com/CPAN/doc/FAQs/FAQ/html/perlfaq4/
          How_can_I_always_kep_my_hash_so.html

Hope this helps!

--
Tom Phoenix           http://www.teleport.com/~rootbeer/

Randal Schwartz Case:  http://www.rahul.net/jeffrey/ovs/



Tue, 11 Jan 2000 03:00:00 GMT  
 Q: associative arrays



Quote:
>Hi,

>another newbie question: Is there a way to force Perl to iterate over
>the elements in an associative array in the order as it was given in
>the array definition?
>Example:

>%dum = ( '3', '3', '1', '1', '2', '2' );

>while (($key, $val) = each(%dum)) {
>   print "$key/$val\n";
>}

Once the data have been hashed their order as returned by perl's builtins
is determined by the hashing and iterating mechanism.  It's nit possible
to do what you ask directly, but you might be able to build a noremal
(ordered) list while constructing the array e.g.



or make each element of %dum a tuple with elements for storing the data or
storing some information to sort by.

You could look at the Tie::Hash documentation to see if you want to
implement a tied array which remembers which order elements were added and
can "fix" the ordering of keys & values returned.

Hope this helps,

Mike

--

http://www.stok.co.uk/~mike/       |   PGP fingerprint FE 56 4D 7D 42 1A 4A 9C
http://www.tiac.net/users/stok/    |                   65 F3 3F 1D 27 22 B7 41



Tue, 11 Jan 2000 03:00:00 GMT  
 Q: associative arrays

Quote:

> another newbie question: Is there a way to force Perl to iterate over
> the elements in an associative array in the order as it was given in
> the array definition?

        No.  Well, not directly anyway.

Quote:
> Example:
> %dum = ( '3', '3', '1', '1', '2', '2' );
> while (($key, $val) = each(%dum)) {
>    print "$key/$val\n";
> }

        You can use a map array if you must to force the order:



        for (my $i=0; $i < $#dum; $i += 2) {
                print "$dum[$i]/$dum{$dum[$i]}\n";
        }

        Ugly, yes, but it works...

        Another option is to create your own tied hash class that does this
        transparently, but that requires some pretty advanced perl knowlage.
        Check out man perltie if you're up to it.a

Quote:
> You may ask why I do not use normal arrays but I have good reasons not
> to do so ...

        And they would be...what?  If you told us the reasons we may
        (likely) be able to offer other solutions.  Always, TIMTOWTDI in
        perl.

--
-Zenin
 Quake Clan After Shock (Did you feel that?)
 The Bawdy Cast - Rocky Horror Picture Show (San Jose, CA)
 Zenin's Rocky Archive (http://www.best.com/~zenin/)



Tue, 11 Jan 2000 03:00:00 GMT  
 Q: associative arrays

Quote:

> Hi,

> another newbie question: Is there a way to force Perl to iterate over
> the elements in an associative array in the order as it was given in
> the array definition?
> Example:

> %dum = ( '3', '3', '1', '1', '2', '2' );

> while (($key, $val) = each(%dum)) {
>    print "$key/$val\n";
> }

> This prints (not surprisingly):
> 1/1
> 2/2
> 3/3

> but I want to see;

> 3/3
> 1/1
> 2/2

> You may ask why I do not use normal arrays but I have good reasons not

> to do so ...

> Cheers


  From the FAQ ( http://www.perl.com/FAQ ):

Q. How can I make my hash remember the order I put elements into it.
A. Use the Tie::IxHash ( module ) from CPAN ( http://www.perl.com/CPAN )

I haven't used this myself so I don't know anything about it but this
should get you started.

Simon



Tue, 11 Jan 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. associative array or associative arrays

2. Associative array of associative arrays

3. Multidimensional asociative array syntax Qs

4. (Q) array of associative arrays

5. Associative array of associate arrays

6. Array of Associative Arrays?

7. Q: using arrays in an associative array

8. Array of associative arrays?

9. associative arrays of arrays

10. Help with sort routine and array of associative arrays

11. Associative array of arrays

12. Associative array containing arrays

 

 
Powered by phpBB® Forum Software