Solved

returning data into columns from a hash

Posted on 2011-02-25
6
371 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now