[Zope] parsing strings in DTML
Rik Hoekstra
rik.hoekstra@inghist.nl
Mon, 26 Jun 2000 22:06:53 +0200
>
>I'm working on what should be a simple problem.
>
>I pulling an address out of an ldap query and the data is in the form of a
>$-separated string. Example:
>
>101 Main St.$Anytown$MN$12345
>
>Please correct me if I'm wrong, but I could probably do <dtml-call
>"REQUEST.set('parsed_address', _.string.join(old_address, '$')"> except for
>the fact that getting "old_address" (the $-separated one) would require its
>own <dtml-var old_address> statement. I can't nest <dtml>s so how to I
>combine these?
I'm not sure I get you right.
try something like (untested):
<dtml-call "REQUEST.set('parse_address', _string.split(old_address, '$')">
for parsing the string
>
>Second question... Once I've got my string parsed into a list called
>"parsed_string", what's the syntax for accessing certain elements of the
>list?
By slicing:
<dtml-call "parsed_string[0]">
gets you the first element parsed_string[1] the second etc
parsed_string[1:] is everything after the second ;-) element of the list
parsed_string[:5] is everything up to element 6
parsed_string[1:5] is everything from element 2 to element 6
parsed_string[-1] is the last element of the list.
If you want to know more, you should probably look at the Python docs, as
this is Python stuff.
>
>P.S. Maybe I should do this in an external or python method, but I thought
>it would be overkill for a single operation like this. Am I wrong?
No, I think you're right, but others may disagree.
hth
Rik