Confused about manipulating nested lists in DTML
I have a list of lists: [['Washington', 'state'], ['Washington', 'state', 'Auburn'], ...] The first element has length two, the rest length three. If I pass that to a DocumentTemplate instance as a parameter named "data" and manipulate it thus: <!--#var "len(_['data'])"--> <!--#var data--> <!--#in data--> <!--#var "len(_['sequence-item'])"--> <!--#var sequence-item--> <!--#if "len(_['sequence-item']) == 2"--> <p><!--#var "_['sequence-item'][0]"--> <!--#in sequence-item--> <!--#var sequence-item--><br> <!--#/in--> <!--#/if--> <!--#/in--> I get the following output: 9 [['Washington', 'state'], ['Washington', 'state', 'Auburn'], ['Washington', 'state', 'Bainbridge Island'], ['Washington', 'state', 'Bellevue'], ['Washington', 'state', 'Bellingham'], ['Washington', 'state', 'Bremerton'], ['Washington', 'state', 'Chehalis'], ['Washington', 'state', 'Chimacum'], ['Washington', 'state', 'Cosmopolis'], ['Washington', 'state', 'Cowlitz County'], ['Washington', 'state', 'Ellensburg'], ['Washington', 'state', 'Elma'], ['Washington', 'state', 'Everett'], ['Washington', 'state', 'Friday Harbor'], ['Washington', 'state', 'George'], ['Washington', 'state', 'Gig Harbor'], ['Washington', 'state', 'Kennewick'], ['Washington', 'state', 'Kent'], ['Washington', 'state', 'Kirkland'], ['Washington', 'state', 'Lacey'], ['Washington', 'state', 'Lake Stevens'], ['Washington', 'state', 'Long Beach'], ['Washington', 'state', 'Longview'], ['Washington', 'state', 'Moses Lake'], ['Washington', 'state', 'Mount Vernon'], ['Washington', 'state', 'Oak Harbor'], ['Washin! gton', 'state', 'Ocean Shores'], ['Washington', 'state', 'Olympia'], ['Washington', 'state', 'Packwood'], ['Washington', 'state', 'Pasco'], ['Washington', 'state', 'Phila/fort'], ['Washington', 'state', 'Port Angeles'], ['Washington', 'state', 'Port Townsend'], ['Washington', 'state', 'Pullman'], ['Washington', 'state', 'Puyallup'], ['Washington', 'state', 'Redmond'], ['Washington', 'state', 'Renton'], ['Washington', 'state', 'Richland'], ['Washington', 'state', 'Seattle'], ['Washington', 'state', 'Snoqualmie'], ['Washington', 'state', 'Spokane'], ['Washington', 'state', 'Stevenson'], ['Washington', 'state', 'Sunnyside'], ['Washington', 'state', 'Tacoma'], ['Washington', 'state', 'Twisp'], ['Washington', 'state', 'Vancouver'], ['Washington', 'state', 'Walla Walla'], ['Washington', 'state', 'Waterville'], ['Washington', 'state', 'Wenatchee'], ['Washington', 'state', 'Winthrop'], ['Washington', 'state', 'Woodinville'], ['Washington', 'state', 'Yakima']] 18 ['Washington', 'state'] 18 ['Washington', 'state', 'Auburn'] 18 ['Washington', 'state', 'Bainbridge Island'] 18 ['Washington', 'state', 'Bellevue'] 18 ['Washington', 'state', 'Bellingham'] 18 ['Washington', 'state', 'Bremerton'] 18 ['Washington', 'state', 'Chehalis'] 18 ['Washington', 'state', 'Chimacum'] 18 ['Washington', 'state', 'Cosmopolis'] 18 ['Washington', 'state', 'Cowlitz County'] ... *What* objects have lengths of 9 and 18? The outer list is length 33. The inner lists are all length 2 or 3. Obviously I screwed up and misused len(), but how? Thx, Skip Montanaro | Mojam: "Uniting the World of Music" http://www.mojam.com/ skip@mojam.com | Musi-Cal: http://www.musi-cal.com/ 518-372-5583
<!--#var "len(_['sequence-item'])"--> <!--#var sequence-item--> [snip] 18 ['Washington', 'state'] [snip]
*What* objects have lengths of 9 and 18? The outer list is length 33. The inner lists are all length 2 or 3. Obviously I screwed up and misused len(), but how?
I'm still learning what to do, but I do now what has length of 18: _['sequence-item'] = 18 characters. So seems that its being calculated before the resolution step. -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Tom Jenkins DevIS (http://www.devis.com) Linux & Samba 2.0 : The best Windows file server http://www.zdnet.com/sr/stories/issue/0,4537,2196106,00.html "Do not be overcome by evil, but overcome evil with good." Romans 12:21
At 14:33 15/03/99 , skip@mojam.com wrote:
I have a list of lists:
[['Washington', 'state'], ['Washington', 'state', 'Auburn'], ...]
The first element has length two, the rest length three. If I pass that to a DocumentTemplate instance as a parameter named "data" and manipulate it thus:
<!--#var "len(_['data'])"--> <!--#var data--> <!--#in data--> <!--#var "len(_['sequence-item'])"--> <!--#var sequence-item--> <!--#if "len(_['sequence-item']) == 2"--> <p><!--#var "_['sequence-item'][0]"--> <!--#in sequence-item--> <!--#var sequence-item--><br> <!--#/in--> <!--#/if--> <!--#/in-->
I get the following output:
<<SNIP>>
*What* objects have lengths of 9 and 18? The outer list is length 33. The inner lists are all length 2 or 3. Obviously I screwed up and misused len(), but how?
The function len() is not Python's len(), but Zope's len(), and it only returns lengths on strings.... just count the number of characters in "_['data']" and "len(_['sequence-item'])". You *should* use _.len(): <!--#var "_.len(data)"--> <!--#var data--> <!--#in data--> <!--#var "_.len(_['sequence-item'])"--> <!--#var sequence-item--> <!--#if "_.len(_['sequence-item']) == 2"--> <p><!--#var "_['sequence-item'][0]"--> <!--#in sequence-item--> <!--#var sequence-item--><br> <!--#/in--> <!--#/if--> <!--#/in--> -- M.J. Pieters, Web Developer | Antraciet http://www.antraciet.nl | Tel: +31-35-6254545 Fax: +31-35-6254555 | mailto:mj@antraciet.nl http://www.antraciet.nl/~mj | PGP: http://wwwkeys.nl.pgp.net:11371/pks/lookup?op=get&search=0xA8A32149 ------------------------------------------
Martijn> You *should* use _.len(): Thank you. That should get the cart rolling until it hits the next pothole... ;-) Skip Montanaro | Mojam: "Uniting the World of Music" http://www.mojam.com/ skip@mojam.com | Musi-Cal: http://www.musi-cal.com/ 518-372-5583
participants (3)
-
Martijn Pieters -
skip@mojam.com -
Tom Jenkins