Solved

returning data into columns from a hash

Posted on 2011-02-25
6
392 Views
Last Modified: 2012-05-11
Hello
The following script  further down looks up some sample xml data and data returned to a hash.
How can I now ge the data in the hash returned as columns see example data below..
First of all sample hash data...
{"CD"=>
  [{"TITLE"=>["Empire Burlesque"],
    "ARTIST"=>["Bob Dylan"],
    "COUNTRY"=>["USA"],
    "COMPANY"=>["Columbia"],
    "PRICE"=>["10.90"],
    "YEAR"=>["1985"]},
   {"TITLE"=>["Hide your heart"],
    "ARTIST"=>["Bonnie Tyler"],
    "COUNTRY"=>["UK"],
    "COMPANY"=>["CBS Records"],
    "PRICE"=>["9.90"],
    "YEAR"=>["1988"]},
No what I want is data returned like so,

Title                        Artist             Country        Company      Price       Year
Hide your heart     Bonnie Tyler     UK               CBS Records   9.9        1988
etc
etc



require 'rubygems'
require 'xmlsimple'
require'open-uri'

a=open('http://www.w3schools.com/xml/cd_catalog.xml').read
xml=(a)
doc=XmlSimple.xml_in xml

require 'pp'
pp doc
0
Comment
Question by:philsivyer
  • 3
  • 3
6 Comments
 
LVL 12

Expert Comment

by:JESii
ID: 34983569
I see that your first "TITLE" has an opening square bracket
     [{"TITLE"=>["Empire Burlesque"],
that is not closed anywhere. So is the structure a hash with a single element (key of 'CD' and an array value composed of hashes) or is it something else? I.e., is it:
   {key, [ hash, hash, hash]} or...?
0
 

Author Comment

by:philsivyer
ID: 34983760
Hello
When I introduce this ..
doc.each{|k,v|p v} or doc.each{|k,v|p k} .. it shows the key as CD - this part I am not interested in - it then shows the value as all the remainder opf the xml data - This is the bit I am interested in and split out as per the following
Title                        Artist             Country        Company      Price       Year
Hide your heart     Bonnie Tyler     UK               CBS Records   9.9        1988
etc
etc

It looks like key of CD then hash,hash etc
0
 
LVL 12

Accepted Solution

by:
JESii earned 500 total points
ID: 34997842
After some testing, I see that the format does have the single array, the elements of which are the hashes. See the code below for how I would go about this:
1. Get rid of the outer-most hash and work with the array of hashes
2. Get the column headers from the first element
3. Iterate thru all the hashes, extracting the data.

I've added comments and displays so that you can see what's happening. FYI the 'pp' module is the pretty-printer module in Ruby which gives nice, formatted output as you can see.

HTH...
#!/usr/bin/ruby
require 'pp'

test={"CD"=> [{"TITLE"=>["Empire Burlesque"], "ARTIST"=> ["Bob Dylan"], "COUNTRY"=>["USA"], "COMPANY"=>["Columbia"], "PRICE"=>["10.90"], "YEAR"=>["1985"]}, {"TITLE"=>["Hide your heart"], "ARTIST"=>["Bonnie Tyler"], "COUNTRY"=>["UK"], "COMPANY"=>["CBS Records"], "PRICE"=>["9.90"], "YEAR"=>["1988"]}]}
pp test

# Step 1
albums = test["CD"]
puts "Value[0]..."
pp albums[0]
puts "Value[1]..."
pp albums[1]

result = []
# Step 2
result = albums[0].map { |key, value| key }
puts "Printing result headers..."
pp result

puts "albums.class = " + albums.class.to_s
# Step 3
albums.each_with_index do |album, i|
	puts "Printing each album; type = " + album.class.to_s + " ..."
	pp album
	result << album.map {|key, value| value[0] }
	end

puts "Printing result array..."
pp result

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:philsivyer
ID: 35001611
Thanks very much for your time on this
0
 

Author Closing Comment

by:philsivyer
ID: 35001621
Thanks
Great work
0
 
LVL 12

Expert Comment

by:JESii
ID: 35006983
You are welcome! Thanks for the points...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question