Solved

returning data into columns from a hash

Posted on 2011-02-25
6
388 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Replace text in files 3 486
Java OO Design  - general knowledge 7 486
issues with iptables on ubuntu 12.04LTS and smtp.gmail.com 4 1,175
Deployment to Heroku, code=H10 error 1 704
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…
Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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