Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Newbie with a project.(Parse tabed file and gen statistics?)

Reply
Thread Tools

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

 
 
Aces Ace
Guest
Posts: n/a
 
      12-11-2006
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.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
William James
Guest
Posts: n/a
 
      12-11-2006
Paul Lutus wrote:
> 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
>
> -----------------------------
>
> --
> Paul Lutus
> http://www.arachnoid.com


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Visual Studio isn't generating the Code Gen file (*.designer.vb) Nathan Sokalski ASP .Net 7 05-15-2007 03:16 AM
Visual Studio isn't generating the Code Gen file (*.designer.vb) Nathan Sokalski ASP .Net Web Controls 7 05-15-2007 03:16 AM
Tabed menu in a table sbi.michael.moreyne@gmail.com Javascript 0 11-16-2006 07:13 PM
Re: "Win32:Trojan-gen. {VC}""Win32.trojan-gen.{UPX!}" jamesa01 Computer Support 2 02-27-2006 02:54 PM
"Win32:Trojan-gen. {VC}" "Win32:Trojan-gen. {UPX!}" D@Z Computer Support 5 01-30-2006 07:52 PM



Advertisments