Mat Schaffer
7/19/2006 9:18:00 PM
On Jul 19, 2006, at 4:47 PM, Ricardo Furgeson wrote:
> Hello everybody,
>
> Ruby is my first prgramming lenguage. I know it's a simple
> question so
> I hope someone can help me out. I have a file that i need to read,
> here's my code so far:
>
> class Parser
>
> File.open("Data") do |file|
> while line = file.gets
>
>
>
>
> end
> end
>
> end
>
> somewhere in my file there is a table of strings like this:
>
> "Hello" = "hi miguel"
> "wrong" = "this is not the right anser"
> "correct asnwer" = "this is the right asnwer"
> "for what" = "tell me what you need it for"
>
> My problem is this:
> I want o search for this table, and read in such a way so that when I
> call for a value(example, hello I can get its value, which is 'hi
> miguel').
>
> I know it's a straigt forward process. I'm not sure how to search for
> the table, and wheather to use a hash or arrays. Can anyone help me
> out?
>
> Thank you so much.
>
>
> Ricardo
I find that each_line is nicer to work with than .gets for these
purposes. It would look like this:
File.open("Data") do |file|
file.each_line do |line|
#here pull the line appart (regular expressions are a good
choice, or maybe using the String#split function. Check 'ri' for that)
#Use the pulled apart piece to create a data structure
end
end
I'll leave the middle parts to you. Be warned that the "line"
variable will still have a new line ("\n") at the end. A mistake
which caught me when I was first learning Ruby.
Good luck!
Mat