Howdy!

You are missing the point.

In general, a Perl hash with a given set of keys will produce them in a consistent order when you call keys(). Calling keys() makes the set of keys into a list. That list is inherently ordered, but it's not the hash.

Now, add or delete elements from the hash. There is no assurance that the list returned by keys() will be in the same order. In fact, insertions can change the order of keys that existed before. The set of keys in a hash has no deterministic fixed order. Change the insertion order and the keys come out in a different order. Consider the following:

my @hash_keys = qw/foo bar baz quux w x y z/; my %foo; my %bar; for my $key (@hash_keys) { $foo{$key} = 1; print "$key: ", join(':', keys %foo), "\n"; } for my $key (reverse @hash_keys) { $bar{$key} = 1; print "$key: ", join(':', keys %bar), "\n"; }
which produced the following output:
foo: foo bar: bar:foo baz: bar:baz:foo quux: bar:baz:quux:foo w: w:bar:baz:quux:foo x: w:bar:baz:quux:x:foo y: y:w:bar:baz:quux:x:foo z: y:w:bar:baz:quux:x:foo:z z: z y: y:z x: y:x:z w: w:y:x:z quux: w:y:quux:x:z baz: w:y:baz:quux:x:z bar: w:y:bar:baz:quux:x:z foo: w:baz:x:y:bar:quux:foo:z

If there was an intrinsic sequence, why does adding "foo" to hash cause "x" to suddenly appear much earlier in the list produced by keys()? Why are the lists with all eight keys not the same? If the sequence were intrinsic, one would not expect either of those outcomes.

yours,
Michael

In reply to Re^7: What makes an array sorted and a hash unsorted? by herveus
in thread What makes an array sorted and a hash unsorted? by ikegami

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.