Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

returning data into columns from a hash

Posted on 2011-02-25
6
Medium Priority
?
411 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

715 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