Newbie with a project.(Parse tabed file and gen statistics?)

A

Aces Ace

I have tab delimited files that I want to parse and generate statistics
with. I can parse the file into an array using IO.readlines but don't
know what good that does for sorting, and the other path I was following
was this.

class MyNewClass
File.open("/home/user/testdata") do |openfile|
openfile.each do |iterationshere|
status, keyword, location, state, zip, date, resultcount,
searchcount = iterationshere.chomp.split(/\t+/)
puts "keyword: #{keyword} status: #{status}, location: #{location},
state: #{state}, zip: #{zip}, date: #{date}, resultcount:
#{resultcount}, searchcount :#{searchcount}"
end
end
end


This obviously gos through the file line by line and splits on the tab
character and assigns the the variables status keyword etc then prints
that line.
But how do I put those into a hash and sort on the fields. Eventually I
will need this to go into a database, but I want to take this one step
at a time.
Thanks for helping this newbie. I have been using Perl for a while
(still consider myself newbie with it), but have decided to get more
into web development using the Rails framework and noticed Ruby had
regexp support built in. WOOT.
 
W

William James

Paul said:
Paul Lutus wrote:

/ ...
Experiment with this program:

A hasty correction:

-----------------------------

#!/usr/bin/ruby -w

data = File.read("data.tsv")

database = []

data.each do |line|
database << line.split(/\t/)
end

database = database.sort { |a,b| b[4].to_i <=> a[4].to_i }

puts database

p IO.readlines('junk').map{|s| s.chomp.split("\t")}.sort_by{|a|
a[4].to_i}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top