469,315 Members | 1,802 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

Reading contents of a file and storing into variables

Hi,
I want to read the file content and store them in different
variables.
My file looks something like this:

----------------------------------------------------
url = "http://localhost"
emailid = "User@htmdec.com"
password= "12345678"
browser="iexplore"
-------------------------------------------------

I have to read each line and store only values within quotes into a variable.

eg:
webpage = "http://localhost"
pwd = "12345678"

Let me know if there is any ruby equivalent to this or any other method
to get this done.

Cheers
Aug 14 '09 #1
3 5132
improvcornartist
303 Expert 100+
You can read the file using File.read. Then you will have to get the necessary data out of each line.
Expand|Select|Wrap|Line Numbers
  1. File.read(filename).each{|line|
  2.   # Perform necessary manipulation in here
  3. }
Aug 14 '09 #2
Thanks for your reply, More than reading a file, i wanted to know is there a way to selective pick the information within quotes(" ") and storing such information in a variable.
Is there a regex that i need to implement to get the value or is there any easier way of fetching values and storing them.
Please do give me a example as i am new to ruby.

cheers
Aug 17 '09 #3
improvcornartist
303 Expert 100+
One way to get the info from within quotes would be to split the string on the quote character. This would split your string into three pieces, so you would want the middle one.
Expand|Select|Wrap|Line Numbers
  1. s.split('"')[1]
Aug 17 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by nightflyer | last post: by
6 posts views Thread by Foxy Kav | last post: by
19 posts views Thread by Lionel B | last post: by
29 posts views Thread by yourmycaffiene | last post: by
21 posts views Thread by EdUarDo | last post: by
6 posts views Thread by efrenba | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.