Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Note that each in scalar context returns the next key.

One of the things I rarely use.

each iterates over the keys of a hash, returning different values when called repeatedly.

>perl -E '%h=map{ $_ => 1 } qw( a b c d e f ); while ($k=each %h) { sa +y $k }' a c d b f e >

But the MUMPS $ORDER function is different. Keys in MUMPS are always sorted, unlike Perl's hash keys. And keys must not be empty. $ORDER returns the next key for the key you pass into. It does not iterate. Passing -1 as a second argument to $ORDER returns the previous key instead of the next one. If there is no previous / next key, an empty string is returned.

KILL ^H SET ^H("D")=1 SET ^H("E")=1,^H("F")=1 S ^H("A")=1,^H("B")=1,^H("C")=1 ; ^-- ^H now contains the keys "A", "B", "C", "D", "E", "F" ; WRITE $ORDER(^H("")),! ; ^-- writes "A", the first key (in order) of ^H ; W $O(^H("C")),! ; ^-- writes "D", the next key after "C" in ^H ; W $O(^H("F")),! ; ^-- writes "", because "F" is the last key in ^H ; W $O(^H("D"),-1),! ; ^-- writes "C", the key before "D" in ^H ; W $O(^H("A"),-1),! ; ^-- writes "", because "A" is the first key in ^H ; SET K="" FOR SET K=$ORDER(^H(K)) QUIT:K="" DO .WRITE K WRITE ! ; ^-- writes "ABCDEF", all keys of ^H in order ; S K="" F S K=$O(^H(K,-1)) Q:K="" D .W K W ! ; ^-- writes "FEDCBA", all keys of ^H in reverse order (-1 passed as + second argument to $ORDER) ; S K="B" F S K=$O(^H(K)) Q:K="" Q:K="E" D .W K W ! ; ^-- writes "CD", all keys of ^H following "B" and stopping at the +end of keys or at "E", in order ; S K="B" F S K=$O(^H(K)) Q:K="" Q:K]]"E" D .W K W ! ; ^-- writes "CDE", all keys of ^H following "B" and stopping at the + end of keys or at the first key sorting after "E", in order

Actually, $ORDER is a "new" (*cough*) function, it can handle negative and non-numeric keys. The original function used to iterate over keys was $NEXT, and it was intended to be used only with positive integers, i.e. plain arrays or sparse arrays. It returns -1 if no more keys can be found. Unfortunately, -1 is a legal key. So if $NEXT returns -1, you can not know if you found the last key or you just happen to have -1 as a key. $ORDER fixes that problem, and $NEXT is provided for legacy code from the dark ages.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re^6: The error says the value is uninitialized, but it works anyway by afoken
in thread The error says the value is uninitialized, but it works anyway by mizducky

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

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

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (4)
    As of 2019-10-22 01:46 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?