Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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

In reply to Re^2: What makes an array sorted and a hash unsorted? by ikegami
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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-24 01:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found