The difference is in this code:
print $_ foreach @array;
print $_ foreach keys %hash
So if keys sorted the keys before returning them, the hash would be considered sorted? nah.
Another way to look at is that an array keeps it's sort order, where a hash doesn't.
keys may not return the keys in order, but it could. "f" is not suddenly less than "b" because keys decides to return foo before bar.
Again, it can't be that hashes aren't ordered simply because Perl doesn't provide a builtin function to return them sorted like it does for arrays.
The hash's list will change with every new run.
Nit:
s/will/may/
Perl can and will return them in the same order in some circumstances, even after the 5.8.1 change to add randomisation.
$ perl -e'system(perl => "-le %h = map { \$_=>1 } qw(a b c d); print k
+eys %h") for 1..10'
cabd
cabd
cabd
cabd
cabd
cabd
cabd
cabd
cabd
cabd
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|