Right, I should have shown it as follows:

    <script>testsWin = {'Harmony':0,'DeclinePlus':0, 'Harmony1':0, 
'RTA':0}</script>
 
    <iterate var="test" in="sorted(testsWin.iterkeys())">
      <log message="1">'test=%s' % test</log>
    </iterate>

But, I don't think that Cindy wanted to sort the Map by key.  She wanted 
to sort the map by order of insertion.

--------------------------------------------------------------
Sharon Lucas
IBM Austin,   luc...@us.ibm.com
(512) 286-7313 or Tieline 363-7313




From:   Joseph J Veilleux/Lexington/IBM
To:     Sharon Lucas/Austin/IBM@IBMUS, 
Cc:     Cindy Zhu <c...@fekete.com>, "staf-users@lists.sourceforge.net" 
<staf-users@lists.sourceforge.net>
Date:   10/26/2011 12:11 PM
Subject:        Re: [staf-users] Iterating a dictionary in a STAX job


Sharon, the List.sort() method in Python sorts a list in-place (i.e. it 
operates on the list itself; it does not yield a sorted list as output), 
so I doubt that the suggestion in your second paragraph would work, at 
least not in the specific way you have shown it.

---------------------------------------------------------------------------------------
Joe Veilleux
IBM/Lotus Domino Server Quality Engineering
550 King Street
Littleton MA 01460
Email: joeveill...@us.ibm.com





Sharon Lucas/Austin/IBM@IBMUS 
10/25/2011 04:54 PM

To
Cindy Zhu <c...@fekete.com>
cc
"staf-users@lists.sourceforge.net" <staf-users@lists.sourceforge.net>
Subject
Re: [staf-users] Iterating a dictionary in a STAX job






A dictionary (dict) in Python does not preserve order.  Instead of order, 
a dictionary uses a hashing algorithm to identify each item's place in the 
dictionary for fast lookup.  For more information, google for information 
about Python dictionaries, for example, see 
http://homepage.mac.com/s_lott/books/python/html/p02/p02c05_maps.html. 

You could sort the keys (e.g. testsWin.keys().sort(), but that will sort 
the keys in alphabetical order, not the order in which you defined them. 

Instead, you could use a list as it preserves order (if you didn't really 
need a dictionary).  For example, a list of strings, or a list of 
dictionaries, or what ever you needed.  If you really need to use a 
dictionary, you could do something like described at 
http://stackoverflow.com/questions/60848/how-do-you-retrieve-items-from-a-dictionary-in-the-order-that-theyre-inserted
. 

Here's an example that uses a list of strings: 

<script> 
  testsWin = [ 'Harmony', 'DeclinePlus', 'Harmony1', 'RTA' ] 
</script> 

<iterate var="test" in="testsWin"> 
  <log message="1">'test=%s' % test</log> 
</iterate> 

STAX Job's Message/Log Output: 
--------------------------------------------------- 
20111025-15:36:47 
Info 
test=Harmony 
20111025-15:36:47 
Info 
test=DeclinePlus 
20111025-15:36:47 
Info 
test=Harmony1 
20111025-15:36:47 
Info 
test=RTA


Or you could use a list of dictionaries if you needed to store more 
information about each item in the list.  For example: 

<script> 
  testsWin = [ {'name':'Harmony', 'count':0}, 
               {'name':'DeclinePlus', 'count':0}, 
               {'name':'Harmony1', 'count':0}, 
               {'name':'RTA', 'count':0} 
             ] 
</script> 

<iterate var="test" in="testsWin"> 
  <log message="1">'testName=%s, testCount=%s' % (test['name'], 
test['count'])</log> 
</iterate> 

STAX Job's Message/Log Output: 
--------------------------------------------------- 
20111025-15:36:47 
Info 
testName=Harmony, testCount=0 
20111025-15:36:47 
Info 
testName=DeclinePlus, testCount=0 
20111025-15:36:47 
Info 
testName=Harmony1, testCount=0 
20111025-15:36:47 
Info 
testName=RTA, testCount=0


Note that in Python 2.7, an OrderedDict API was added that provides the 
same interface as regular dictionaries but iterates over keys and values 
in a guaranteed order depending on when a key was first inserted. However, 
STAX V3.5.0 and later uses Jython 2.5.2 which is the latest Jython version 
available and is based on Python 2.5 so it doesn't support the OrderedDict 
API.  Jython development lags behind Python. 

--------------------------------------------------------------
Sharon Lucas
IBM Austin,   luc...@us.ibm.com
(512) 286-7313 or Tieline 363-7313




From:        Cindy Zhu <c...@fekete.com> 
To:        "staf-users@lists.sourceforge.net" 
<staf-users@lists.sourceforge.net>, 
Date:        10/25/2011 03:05 PM 
Subject:        [staf-users] Iterating a dictionary in a STAX job 



Hi,

I have the following STAX job to iterate a dictionary and print out the 
keys in the dictionary.

<script>
                testsWin = {'Harmony':0,'DeclinePlus':0, 'Harmony1':0, 
'RTA':0}
</script>

<defaultcall function="main" />

<function name="main" scope="local">
                <sequence>
                                 <iterate var="test" in="testsWin.keys()">
                                 <log message="1">'test=%s' % test</log>
                                 </iterate>
                </sequence>
</function>

The log message shows in this order: Harmony1, DeclinePlus, RTA, Harmony.

Is there a way to make the print out(message) has the same key order as 
that in variable testsWin?

Thanks,

Cindy

   

------------------------------------------------------------------------------
The demand for IT networking professionals continues to grow, and the
demand for specialized networking skills is growing even more rapidly.
Take a complimentary Learning@Cisco Self-Assessment and learn 
about Cisco certifications, training, and career opportunities. 
http://p.sf.net/sfu/cisco-dev2dev
_______________________________________________
staf-users mailing list
staf-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/staf-users

------------------------------------------------------------------------------
The demand for IT networking professionals continues to grow, and the
demand for specialized networking skills is growing even more rapidly.
Take a complimentary Learning@Cisco Self-Assessment and learn 
about Cisco certifications, training, and career opportunities. 
http://p.sf.net/sfu/cisco-dev2dev
_______________________________________________
staf-users mailing list
staf-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/staf-users

------------------------------------------------------------------------------
The demand for IT networking professionals continues to grow, and the
demand for specialized networking skills is growing even more rapidly.
Take a complimentary Learning@Cisco Self-Assessment and learn 
about Cisco certifications, training, and career opportunities. 
http://p.sf.net/sfu/cisco-dev2dev
_______________________________________________
staf-users mailing list
staf-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/staf-users

Reply via email to