Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get hash keys in order of their appearance

Tags:

hash

raku

I have a hash:

use v6;

my %some-hash = a => 0,
                b => 42,
                c => 417;

and I'm trying to get its keys with %some-hash.keys, which returns a Seq of all keys but not in the order in which they were declared. It seems that the order of keys is determined at hash initialization, because it changes if I run the code several times.

Is it possible to preserve the order (a b c)?

P.S. %some-hash.keys.sort will not suffice, since keys are expected to have arbitrary names.

like image 506
Sergey Suvorov Avatar asked Sep 26 '20 05:09

Sergey Suvorov


People also ask

What is a hash key without hashing applied?

A hash key without the actual hashing applied basically: a single attribute created from the concatenation of the required attributes (including hard-coded values) and sanding delimiters. I’ve looked at it in some detail and can’t really fault it.

What are hash keys and why are they important?

The hash keys also made it possible to truncate (and reload) your Dimension without having to also reload your Fact table, and to load data in essentially any order. Using hash keys has opened up our collective minds to implementing parallel loading, the ability to load data in any order and considering how to easily scale out.

Are the values in a hash map always unique?

For one the mapping of the hash is one-directional: from key to value, but maybe more importantly, the values are not required to be unique. In our example, both Pluto and Charon are at the same average distance from the Sun. What is more likely is that we wanted to Sort the names of the planets according to their distance form the Sun .

How to get the key of hashtable in Java?

The java.util.Hashtable.keys() method of Hashtable class in Java is used to get the enumeration of the keys present in the hashtable. Syntax: Parameters: The method does not take any parameters. Return value: The method returns an enumeration of the keys of the Hashtable.


1 Answers

Hash keys are randomly ordered, as you have seen.

There are modules in the ecosystem that could help you get what you want:

  • OrderedHash
  • ArrayHash

Or if these don't do exactly what you want, you can build your own Hash implementation with:

  • Hash::Agnostic
like image 163
Elizabeth Mattijsen Avatar answered Sep 23 '22 02:09

Elizabeth Mattijsen