Solved

returning data into columns from a hash

Posted on 2011-02-25
6
381 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

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…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

810 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