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

Discussion in 'Ruby' started by Aces Ace, Dec 11, 2006.

  1. Aces Ace

    Aces Ace Guest

    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/.
     
    Aces Ace, Dec 11, 2006
    #1
    1. Advertising

  2. 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}
     
    William James, Dec 11, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Watt

    easy gen 2 and mysql connection

    Paul Watt, Nov 13, 2005, in forum: HTML
    Replies:
    2
    Views:
    752
    Jerry Stuckle
    Nov 14, 2005
  2. Bart Nessux
    Replies:
    4
    Views:
    436
    Tim Roberts
    Jan 10, 2004
  3. Nathan Sokalski
    Replies:
    7
    Views:
    617
    Nathan Sokalski
    May 15, 2007
  4. Nathan Sokalski

    Visual Studio isn't generating the Code Gen file (*.designer.vb)

    Nathan Sokalski, May 12, 2007, in forum: ASP .Net Web Controls
    Replies:
    7
    Views:
    181
    Nathan Sokalski
    May 15, 2007
  5. Tabed menu in a table

    , Nov 16, 2006, in forum: Javascript
    Replies:
    0
    Views:
    130
Loading...

Share This Page