From: Neil W. <neilw@ActiveState.com> - 2002-09-13 21:44:57
|
Steve Howell [13/09/02 17:25 -0400]: > Here's my challenge to you, Oren. Take this data here, put it in vim, add > dashes in front of all the keys, then send it back to me in email. I used this: :%s,^\s*\([^:]\+\):,- \1: --- - Name: Steve Howell - Hometown: Columbia, MD - Languages: [Python, Ruby, Java] --- - First name: Brian - Last name: Ingerson - Language: Perl --- - Last Name: Ganapathy - First Name: Vijay - Birthplace: India - Languages: - English - French > I bet you don't accept the challenge. It's beneath you. It's a pain in the > butt. What intelligent human being wants to do work that a computer could do > just as easily? If the data is generated by a computer, it can do this for you; if it's written by a human, they can type it correctly the first time around. That seems acceptable. If you're using this in an application where you want to use YAML to build some kind of picture of the data to show a user (say, a YAML document which is dynamically turned into a data-entry GUI), then you do care about order, because people are used to seeing data entry boxes in a logical order. If this is the case, then a sequence of maps is perfectly natural, and actually very easy to deal with in languages like Python: for attrib in person: # In Perl: ($k, $v) = each %attrib; k = attrib.keys[0] # is that right? v = attrib.values[0] # ditto? gui.add_entry_box('label': k, 'value': v) Preferably, you could load this into a 'person' class and do this: for attrib in person: gui.add_entry_box('label': attrib.label, 'value': attrib.value) I think that gets the job done. Please pardon any Python booboos. Later, Neil |