[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

need help sorting hashes and yaml

Bucco

5/29/2006 3:27:00 PM

I have an xml file that looks something like this:
<action>
<date item="052906">
<task>Some task</task>
<task>Some other task</task>
</date>
<date item="053006">
... more tasks
</date>
</action>

I am trying to sort these task files into yaml that looks like this:
---
date:
052906:
- Some task
- Some other task
.... and so on for each date/task

I have done the following so far:
require 'xmlsimple'
require 'yaml'
doc = XmlSimple.xml_in("Some file.xml", {'KeyAttr' => 'item'})
puts doc.to_yaml


Although this prints out the correct format, the dates are unsorted.
And some of the dates are in quotes while others are not quoted in the
yaml output. I also tried the follwoing:

puts doc.sort.to_yaml

This sorts the hash into multiple arrays which then adds extra dashes
to the yaml output, but the date order is sorted correctly.

So my question is how to sort the hash so that the dates are read in
the correct order when outputted to yaml, but also keep the hash format
and not convert it to an array to sort?

Thanks in advance:)
SA

6 Answers

Tim Hunter

5/29/2006 4:12:00 PM

0

Bucco wrote:
> I have an xml file that looks something like this:
> <action>
> <date item="052906">
> <task>Some task</task>
> <task>Some other task</task>
> </date>
> <date item="053006">
> ... more tasks
> </date>
> </action>
>
> I am trying to sort these task files into yaml that looks like this:
> ---
> date:
> 052906:
> - Some task
> - Some other task
> ... and so on for each date/task
>
> I have done the following so far:
> require 'xmlsimple'
> require 'yaml'
> doc = XmlSimple.xml_in("Some file.xml", {'KeyAttr' => 'item'})
> puts doc.to_yaml
>
>
> Although this prints out the correct format, the dates are unsorted.
> And some of the dates are in quotes while others are not quoted in the
> yaml output. I also tried the follwoing:
>
> puts doc.sort.to_yaml
>
> This sorts the hash into multiple arrays which then adds extra dashes
> to the yaml output, but the date order is sorted correctly.
>
> So my question is how to sort the hash so that the dates are read in
> the correct order when outputted to yaml, but also keep the hash format
> and not convert it to an array to sort?
>
> Thanks in advance:)
> SA
>

You can't sort the hash. What you can do is use the #keys method to get
an array of the keys, sort the keys array, and then use the sorted keys
array to iterate over the hash.

Bucco

5/29/2006 5:22:00 PM

0

Could you give an example?

Thanks:)
SA

Tim Hunter

5/29/2006 6:10:00 PM

0

Bucco wrote:
> Could you give an example?
>
> Thanks:)
> SA
>
Sure.

irb(main):005:0> h = {'green'=>31, 'purple'=>12, 'red'=>23, 'yellow'=>40}
=> {"green"=>31, "red"=>23, "yellow"=>40, "purple"=>12}
irb(main):006:0> h.keys.sort.each {|k| puts h[k]}
31
12
23
40
=> ["green", "purple", "red", "yellow"]

Trans

5/29/2006 6:43:00 PM

0

I pretty sure YAML has support for the secondary YAML type of ordered
map. Try this:

date: !omap
- 052906:
- Some task
- Some other task
...

Going the other way around of course is trickier but you should be able
to reuse YAML's omap class.

T.

Bucco

5/29/2006 7:39:00 PM

0

The problem I am having is sorting out each days task by date from xml
and then printing them back out in a yaml format. xml_simple pulls the
xml into a hash with the date keys in random order. I need to re-order
the date keys, and then print them to yaml with the correct tasks
underneath each date.

Thanks:)

SA

Trans

5/29/2006 7:48:00 PM

0


> xml_simple pulls the xml into a hash with the date keys in random order.

I wouldn't use xml_simple than since that isn't good behavior --XML
documents are order significant. I'm actually suprised it uses a hash.
You might consider using REXML instead. It's pretty easy to traverse a
document.

T.