Pulling values from strings

Discussion in 'Ruby' started by Michael Furmaniuk, Apr 14, 2009.

  1. I have a string of data that I am trying to pull values from and have
    hit a roadblack since I am still getting the hang of Ruby, in Perl this
    would be easier but it seems that Ruby may be able to do this much
    simpler.

    My data starts as -
    send data StartTime: 20090413 13:41:53.000 ScriptName:changeTests
    Pass:15 Fail:0 Total:15

    Then I am able to regex it to something like -
    ScriptName:changeTests Pass:15 Fail:0 Total:15

    The code I have is:
    if line =~ /data/
    # Remove everything from the beginning up to ScriptName
    line.sub!(/^send.*\d\d\d\s/,"")
    # Split the line by space, then get pairs by :
    arr = line.split(/\s/).map do |i|
    t = i.sub(/[a-zA-Z]+\:([a-zA-Z]+|\d{2,3})/)
    end
    end

    After the arr = line.split I am not sure what to do, I was playing
    around with various ways of pulling the data out but I am not totally
    clear on Ruby data structures. What I would like to do is pull out all
    the values to the right of the :'s and save them for a SQL insert
    statement, this is data I would like to store in a database after
    pulling out the values I need. Is it easier to use map to be able to
    iterate through the values, or can I actually split the data line so
    that I can end up with each combined value sort of acting like a hash
    key? I'd appreciate some hints on what might be a good next step so
    that after arr I should be able to have values like:
    ScriptName = changeTests
    Pass = 15
    Fail = 0
    Total = 15

    Thanks
    --
    Posted via http://www.ruby-forum.com/.
    Michael Furmaniuk, Apr 14, 2009
    #1
    1. Advertising

  2. Michael Furmaniuk

    Robert Dober Guest

    On Tue, Apr 14, 2009 at 8:59 PM, Michael Furmaniuk <> w=
    rote:
    > I have a string of data that I am trying to pull values from and have
    > hit a roadblack since I am still getting the hang of Ruby, in Perl this
    > would be easier but it seems that Ruby may be able to do this much
    > simpler.
    >
    > My data starts as -
    > =A0send data StartTime: 20090413 13:41:53.000 ScriptName:changeTests
    > Pass:15 Fail:0 Total:15
    >
    > Then I am able to regex it to something like -
    > =A0ScriptName:changeTests Pass:15 Fail:0 Total:15
    >
    > The code I have is:
    > =A0 =A0 =A0 =A0if line =3D~ /data/
    > =A0 =A0 =A0 =A0 =A0 =A0# Remove everything from the beginning up to Scrip=

    tName
    > =A0 =A0 =A0 =A0 =A0 =A0line.sub!(/^send.*\d\d\d\s/,"")
    > =A0 =A0 =A0 =A0 =A0 =A0# Split the line by space, then get pairs by :
    > =A0 =A0 =A0 =A0 =A0 =A0arr =3D line.split(/\s/).map do |i|
    > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0t =3D i.sub(/[a-zA-Z]+\:([a-zA-Z]+|\d{2,3}=

    )/)
    > =A0 =A0 =A0 =A0 =A0 =A0end
    > =A0 =A0 =A0 =A0end
    >
    > After the arr =3D line.split I am not sure what to do, I was playing
    > around with various ways of pulling the data out but I am not totally
    > clear on Ruby data structures. =A0What I would like to do is pull out all
    > the values to the right of the :'s and save them for a SQL insert
    > statement, this is data I would like to store in a database after
    > pulling out the values I need. =A0Is it easier to use map to be able to
    > iterate through the values, or can I actually split the data line so
    > that I can end up with each combined value sort of acting like a hash
    > key? =A0I'd appreciate some hints on what might be a good next step so
    > that after arr I should be able to have values like:
    > ScriptName =3D changeTests
    > Pass =3D 15
    > Fail =3D 0
    > Total =3D 15

    Not tested but maybe you can fix errors ;)

    x.split(/\s*(\w+):\s*/)[-8..-1].each_cons(2).map{ |k,v| "#{k} =3D #{v}" }

    which gives you data like

    ["ScriptName =3D changeTests", "changeTests =3D Pass", "Pass =3D 15", "15 =
    =3D
    Fail", "Fail =3D 0", "0 =3D Total", "Total =3D 15"]

    which if printed with puts gives you the output above.

    Note also that instead of that you could create a Hash

    Hash[ *x.split(/\s*(\w+):\s*/)[-8..-1]]

    HTH
    Robert
    >
    > Thanks
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >




    --=20
    Si tu veux construire un bateau ...
    Ne rassemble pas des hommes pour aller chercher du bois, pr=E9parer des
    outils, r=E9partir les t=E2ches, all=E9ger le travail=85 mais enseigne aux
    gens la nostalgie de l=92infini de la mer.

    If you want to build a ship, don=92t herd people together to collect
    wood and don=92t assign them tasks and work, but rather teach them to
    long for the endless immensity of the sea.

    --
    Antoine de Saint-Exup=E9ry
    Robert Dober, Apr 14, 2009
    #2
    1. Advertising

  3. Robert Dober wrote:
    > x.split(/\s*(\w+):\s*/)[-8..-1].each_cons(2).map{ |k,v| "#{k} = #{v}" }


    Scan is also useful for this kind of thing...

    s = "ScriptName:changeTests Pass:15 Fail:0 Total:15"
    p s.scan(/(\w+):(\S+)/)
    # ==> [["ScriptName", "changeTests"], ["Pass", "15"], ["Fail", "0"],
    ["Total", "15"]]

    To deal with the full string:

    s = "send data StartTime: 20090413 13:41:53.000 ScriptName:changeTests
    Pass:15 Fail:0 Total:15"
    a = s.scan(/(\w+):\s*([\s\d:.]+|\S+)/)
    p a
    # ==> [["StartTime", "20090413 13:41:53.000 "], ["ScriptName",
    "changeTests"], ["Pass", "15 "], ["Fail", "0 "], ["Total", "15"]]
    a.shift
    a.each {|key,val| puts "#{key} = #{val}"}

    # ==>
    # ScriptName = changeTests
    # Pass = 15
    # Fail = 0
    # Total = 15

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, Apr 14, 2009
    #3
  4. Michael Furmaniuk

    Robert Dober Guest

    Seems I used only the second best tool for the job ;).
    R.
    Robert Dober, Apr 15, 2009
    #4
  5. Hmm....didn't realize I had this many options for it all.

    Thanks...of course now I have to study these so I can understand what is
    going on...ahh the learning!

    --
    Posted via http://www.ruby-forum.com/.
    Michael Furmaniuk, Apr 15, 2009
    #5
    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. Temp
    Replies:
    1
    Views:
    1,086
    Natty Gur
    Aug 11, 2003
  2. Dave
    Replies:
    1
    Views:
    393
    Lester Botello
    Nov 18, 2003
  3. Ben Davies
    Replies:
    3
    Views:
    343
    Erik Max Francis
    Jan 14, 2004
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    730
    Malcolm
    Jun 24, 2006
  5. RedGrittyBrick

    Pulling pairs of values from an array

    RedGrittyBrick, Dec 17, 2010, in forum: Perl Misc
    Replies:
    3
    Views:
    154
Loading...

Share This Page