Search This Blog

Saturday, October 16, 2010

Ruby: Hash Assignment/Parse from Text

Programmer Question

Looking to create a hash table from a text output that looks like this (whitespace between words are tabs):



GCOLLECTOR     123456     77889     uno  
BLOCK unique111 error fullunique111 ... ... ...
DAY ... ... ...
LABEL detail unique111 Issue Broken - The truck broke
LABEL detail unique111 Folder 3c1
LABEL detail unique111 Datum bar_1666.9
GCOLLECTOR 234567 77889 uno
BLOCK unique222 error fullunique111 ... ... ...
DAY ... ... ...
DAY ... ... ...
LABEL detail unique222 Issue Broken - The truck broke
LABEL detail unique222 Datum bar_9921.2
LABEL detail unique222 Folder 6a3
GCOLLECTOR 345678 77889 uno
BLOCK unique333 error fullunique111 ... ... ...
LABEL detail unique333 Datum bar_7766.2
LABEL detail unique333 Folder 49k
LABEL detail unique333 Issue Broken - The truck broke


I would like to create a hash table that assigns each of the following to the hash:

gcollectors = Hash.new

gcollectors = { "UniqueID" => uniqueXXX,

"Datum" => bar_XXXX.X,

"FullUniqueID" => fulluniqueXXX,

"IssueGroup" => Broken

}



The uniqueXXX fields always match for the BLOCK and associated LABELs.



I am having a couple issues:

1- How do I assign just those fields to the hashes?

2- How can I split the text prior to the hyphen (in LABEL ... Issue) and assign it to IssueGroup?

3- How can this be done reliably when the order of the LABEL lines is different?

.. same question for when there are multiple DAY lines or no DAY lines.



Find the answer here

No comments:

Post a Comment

Related Posts with Thumbnails