String parse

Discussion in 'Ruby' started by anitawa, Aug 14, 2007.

  1. anitawa

    anitawa Guest

    Hello,

    I want to be able to parse a string and put them into variables.

    for example, I have this string:

    "Menu: steak_and_egg | date: 0814 | who: Anita"

    I want to parse this string to assign variables like so:

    menu = "steak_and_egg"
    date = "0814"
    who = "Anita"

    What would be the fastest way of doing this.

    Thanks
    anitawa, Aug 14, 2007
    #1
    1. Advertising

  2. On Aug 14, 2007, at 6:00 PM, anitawa wrote:

    > Hello,
    >
    > I want to be able to parse a string and put them into variables.
    >
    > for example, I have this string:
    >
    > "Menu: steak_and_egg | date: 0814 | who: Anita"
    >
    > I want to parse this string to assign variables like so:
    >
    > menu = "steak_and_egg"
    > date = "0814"
    > who = "Anita"
    >
    > What would be the fastest way of doing this.


    This might work if your data isn't too complicated:

    >> str = "Menu: steak_and_egg | date: 0814 | who: Anita"

    => "Menu: steak_and_egg | date: 0814 | who: Anita"
    >> Hash[*str.scan(/(\w+):\s*(\w+)/).flatten]

    => {"date"=>"0814", "who"=>"Anita", "Menu"=>"steak_and_egg"}

    James Edward Gray II
    James Edward Gray II, Aug 15, 2007
    #2
    1. Advertising

  3. James Edward Gray II wrote:
    > On Aug 14, 2007, at 6:00 PM, anitawa wrote:
    >
    >> Hello,
    >>
    >> I want to be able to parse a string and put them into variables.
    >>
    >> for example, I have this string:
    >>
    >> "Menu: steak_and_egg | date: 0814 | who: Anita"
    >>
    >> I want to parse this string to assign variables like so:
    >>
    >> menu = "steak_and_egg"
    >> date = "0814"
    >> who = "Anita"
    >>
    >> What would be the fastest way of doing this.

    >
    > This might work if your data isn't too complicated:
    >
    > >> str = "Menu: steak_and_egg | date: 0814 | who: Anita"

    > => "Menu: steak_and_egg | date: 0814 | who: Anita"
    > >> Hash[*str.scan(/(\w+):\s*(\w+)/).flatten]

    > => {"date"=>"0814", "who"=>"Anita", "Menu"=>"steak_and_egg"}


    Might be more general than is needed. If you know in advance that the
    "fields" are menu, date, and who, then this will do:

    str = "Menu: steak_and_egg | date: 0814 | who: Anita"
    pat = /Menu: (\S+) \| date: (\S+) \| who: (\S+)/

    menu, date, who = str.scan(pat)[0]

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, Aug 15, 2007
    #3
  4. Le mercredi 15 ao=FBt 2007 01:00, anitawa a =E9crit=A0:
    > Hello,
    >
    > I want to be able to parse a string and put them into variables.
    >
    > for example, I have this string:
    >
    > "Menu: steak_and_egg | date: 0814 | who: Anita"
    >
    > I want to parse this string to assign variables like so:
    >
    > menu =3D "steak_and_egg"
    > date =3D "0814"
    > who =3D "Anita"
    >
    > What would be the fastest way of doing this.
    >
    > Thanks


    This is a CSV format, You can use the csv lib from the stdlib :

    # parse with : and | as delimiters
    str =3D "Menu: steak_and_egg | date: 0814 | who: Anita"
    parsed =3D CSV.parse(str, ":", "|")
    =3D> [["Menu", " steak_and_egg "], [" date", " 0814 "], [" who", " Anita"]]

    # then, put the results in a hash
    res =3D {}
    parsed.each do |k, v|
    res[k.strip] =3D v.strip
    end
    =3D> {"date"=3D>"0814", "who"=3D>"Anita", "Menu"=3D>"steak_and_egg"}

    You can also use Enumerable#inject for putting in the Hash (or Hash::[] wit=
    h=20
    some adaptations)

    =2D-=20
    Olivier Renaud
    Olivier Renaud, Aug 15, 2007
    #4
  5. anitawa

    anitawa Guest

    On Aug 14, 8:12 pm, Joel VanderWerf <> wrote:
    > James Edward Gray II wrote:
    >
    >
    >
    > > On Aug 14, 2007, at 6:00 PM, anitawa wrote:

    >
    > >> Hello,

    >
    > >> I want to be able to parse a string and put them into variables.

    >
    > >> for example, I have this string:

    >
    > >> "Menu: steak_and_egg | date: 0814 | who: Anita"

    >
    > >> I want to parse this string to assign variables like so:

    >
    > >> menu = "steak_and_egg"
    > >> date = "0814"
    > >> who = "Anita"

    >
    > >> What would be the fastest way of doing this.

    >
    > > This might work if your data isn't too complicated:

    >
    > > >> str = "Menu: steak_and_egg | date: 0814 | who: Anita"

    > > => "Menu: steak_and_egg | date: 0814 | who: Anita"
    > > >> Hash[*str.scan(/(\w+):\s*(\w+)/).flatten]

    > > => {"date"=>"0814", "who"=>"Anita", "Menu"=>"steak_and_egg"}

    >
    > Might be more general than is needed. If you know in advance that the
    > "fields" are menu, date, and who, then this will do:
    >
    > str = "Menu: steak_and_egg | date: 0814 | who: Anita"
    > pat = /Menu: (\S+) \| date: (\S+) \| who: (\S+)/
    >
    > menu, date, who = str.scan(pat)[0]
    >
    > --
    > vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


    Thanks, just what i was looking for
    anitawa, Aug 15, 2007
    #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. Vincent

    parse string

    Vincent, Dec 16, 2003, in forum: Perl
    Replies:
    2
    Views:
    2,814
    Gunnar Hjalmarsson
    Dec 16, 2003
  2. Kevin Joseph
    Replies:
    5
    Views:
    4,341
    Joe Smith
    Aug 13, 2004
  3. Henri
    Replies:
    2
    Views:
    318
    Henri
    Nov 29, 2004
  4. Replies:
    19
    Views:
    1,127
    Daniel Vallstrom
    Mar 15, 2005
  5. 7stud --

    optparse: parse v. parse! ??

    7stud --, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    185
    7stud --
    Feb 20, 2008
Loading...

Share This Page