From: Simone D'Amico on
Hello guys,
I have to parse a file of questions/answers for a quizbot in ruby.
The file is structured in this way:
"Question" "Answer"\r\n
"Question2" "Answer2"\r\n
"Questionn" "Answern"\r\n

How to parse it efficacely?
Thank you in advance.



Simone D'Amico
*nix powered
sim(a)me.com





From: Rajinder Yadav on
Simone D'Amico wrote:
> Hello guys,
> I have to parse a file of questions/answers for a quizbot in ruby.
> The file is structured in this way:
> "Question" "Answer"\r\n
> "Question2" "Answer2"\r\n
> "Questionn" "Answern"\r\n
>
> How to parse it efficacely?
> Thank you in advance.
>
> Simone D'Amico
> *nix powered
> sim(a)me.com
>

here is the basic code, output followed by a basic explanation

IO.foreach("testfile.txt") do |line|
data=line.match /\"(\w+)\"\s+\"(\w+)\"/
puts data[1] # question
puts data[2] # answer
end


[output]

Question
Answer
Question2
Answer2
Questionn
Answern


basic explanation: match returns a MatchData object, which can be
accessed like an array.

you will want to access the 2nd and 3rd position of the MatchData
object, the first (zero position) holds an array of the entire parsed
data set.

irb(main):337:0> str='"Question" "Answer"'

irb(main):338:0> data=str.match /\"(\w+)\"\s+\"(\w+)\"/

irb(main):339:0> data[0]
=> "\"Question\" \"Answer\""

irb(main):340:0> data[1]
=> "Question"

irb(main):341:0> data[2]
=> "Answer"


--
Kind Regards,
Rajinder Yadav

http://DevMentor.org

Do Good! - Share Freely

From: Hassan Schroeder on
On Sat, Dec 26, 2009 at 9:03 AM, Rajinder Yadav <devguy.ca(a)gmail.com> wrote:

> here is the basic code, output followed by a basic explanation

>   data=line.match /\"(\w+)\"\s+\"(\w+)\"/

?? That won't remotely work, unless the "Question" and "Answer"
are both single words, which seems unlikely :-)

Try something like line.chomp.match /\"([^"]+)\"\s+\"([^"]+)\"/

>> '"What is the temperature?" "Pretty cold"'.match /\"(\w+)\"\s+\"(\w+)\"/
=> nil
>> '"What is the temperature?" "Pretty cold"'.match /\"([^"]+)\"\s+\"([^"]+)\"/
=> #<MatchData "\"What is the temperature?\" \"Pretty cold\"" 1:"What
is the temperature?" 2:"Pretty cold">
>> result = '"What is the temperature?" "Pretty cold"'.match /\"([^"]+)\"\s+\"([^"]+)\"/
=> #<MatchData "\"What is the temperature?\" \"Pretty cold\"" 1:"What
is the temperature?" 2:"Pretty cold">
>> result[1]
=> "What is the temperature?"
>> result[2]
=> "Pretty cold"
>>

--
Hassan Schroeder ------------------------ hassan.schroeder(a)gmail.com
twitter: @hassan

From: Rajinder Yadav on
Hassan Schroeder wrote:
> On Sat, Dec 26, 2009 at 9:03 AM, Rajinder Yadav <devguy.ca(a)gmail.com> wrote:
>
>> here is the basic code, output followed by a basic explanation
>
>> data=line.match /\"(\w+)\"\s+\"(\w+)\"/
>
> ?? That won't remotely work, unless the "Question" and "Answer"
> are both single words, which seems unlikely :-)
>
> Try something like line.chomp.match /\"([^"]+)\"\s+\"([^"]+)\"/
>
>>> '"What is the temperature?" "Pretty cold"'.match /\"(\w+)\"\s+\"(\w+)\"/
> => nil
>>> '"What is the temperature?" "Pretty cold"'.match /\"([^"]+)\"\s+\"([^"]+)\"/
> => #<MatchData "\"What is the temperature?\" \"Pretty cold\"" 1:"What
> is the temperature?" 2:"Pretty cold">
>>> result = '"What is the temperature?" "Pretty cold"'.match /\"([^"]+)\"\s+\"([^"]+)\"/
> => #<MatchData "\"What is the temperature?\" \"Pretty cold\"" 1:"What
> is the temperature?" 2:"Pretty cold">
>>> result[1]
> => "What is the temperature?"
>>> result[2]
> => "Pretty cold"
>

I shouldn't reply when I am half awake =)

--
Kind Regards,
Rajinder Yadav

http://DevMentor.org

Do Good! - Share Freely

From: Simone D'Amico on
Thank you very much.

Il giorno 26/dic/2009, alle ore 18.20, Hassan Schroeder ha scritto:

> On Sat, Dec 26, 2009 at 9:03 AM, Rajinder Yadav <devguy.ca(a)gmail.com> wrote:
>
>> here is the basic code, output followed by a basic explanation
>
>> data=line.match /\"(\w+)\"\s+\"(\w+)\"/
>
> ?? That won't remotely work, unless the "Question" and "Answer"
> are both single words, which seems unlikely :-)
>
> Try something like line.chomp.match /\"([^"]+)\"\s+\"([^"]+)\"/
>
>>> '"What is the temperature?" "Pretty cold"'.match /\"(\w+)\"\s+\"(\w+)\"/
> => nil
>>> '"What is the temperature?" "Pretty cold"'.match /\"([^"]+)\"\s+\"([^"]+)\"/
> => #<MatchData "\"What is the temperature?\" \"Pretty cold\"" 1:"What
> is the temperature?" 2:"Pretty cold">
>>> result = '"What is the temperature?" "Pretty cold"'.match /\"([^"]+)\"\s+\"([^"]+)\"/
> => #<MatchData "\"What is the temperature?\" \"Pretty cold\"" 1:"What
> is the temperature?" 2:"Pretty cold">
>>> result[1]
> => "What is the temperature?"
>>> result[2]
> => "Pretty cold"
>>>
>
> --
> Hassan Schroeder ------------------------ hassan.schroeder(a)gmail.com
> twitter: @hassan
>



Simone D'Amico
*nix powered
sim(a)me.com