Reading contents of a file and storing

Guys,
         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 individual lines but store only values within the quotes
(values after =)
I heard in java there is some get property which picks values after '='
and so on.
Let me know if there is any ruby equivalent to this or any other method
to get this done.

eg:

variables id = User@htmdec.com & so on..

cheers

···

--
Posted via http://www.ruby-forum.com/.

A lot of people use YAML, a different format like this:

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

However personally I dislike YAML because it's very data-sensitive (note
the need for quotes around the password above) and sometimes buggy.

If you google for "ruby ini file" you'll find some code which probably
does what you need. Otherwise, it's pretty easy to roll your own:

conf = {}
File.open("conf.ini") do |source|
  source.each_line do |line|
    if line =~ /^(\w+)\s*=\s*"(.*)"\s*$/
      conf[$1] = $2
    end
  end
end

Here I build a Hash, so you can access it as

  conf['url']

But you could build an OpenStruct instead (look at ostruct.rb) so you
could access it as conf.url

···

--
Posted via http://www.ruby-forum.com/.

I heard in java there is some get property which picks values after '='
and so on.
Let me know if there is any ruby equivalent to this or any other method
to get this done.

Another way.
Modify as necessary.

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

require 'shellwords'
include Shellwords
h = Hash[*(shellwords(source.gsub("="," ")))]

# {"url"=>"http://localhost", "emailid"=>"User@htmdec.com",
"browser"=>"iexplore", "password"=>"12345678"}

Harry

···

--
A Look into Japanese Ruby List in English

You can save one level of indentation by using

File.foreach "conf.ini" do |line|
...
end

And if you want to get really short (not necessarily recommended):

conf = {}
File.foreach "conf.ini" do |line|
  conf[$1] = $2 if /^\s*(\w+)\s*=\s*"([^"]*)"\s*$/ =~ line
end

Kind regards

robert

···

2009/8/10 Brian Candler <b.candler@pobox.com>:

A lot of people use YAML, a different format like this:

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

However personally I dislike YAML because it's very data-sensitive (note
the need for quotes around the password above) and sometimes buggy.

If you google for "ruby ini file" you'll find some code which probably
does what you need. Otherwise, it's pretty easy to roll your own:

conf = {}
File.open("conf.ini") do |source|
source.each_line do |line|
if line =~ /^(\w+)\s*=\s*"(.*)"\s*$/
conf[$1] = $2
end
end
end

Here I build a Hash, so you can access it as

conf['url']

But you could build an OpenStruct instead (look at ostruct.rb) so you
could access it as conf.url

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Harry Kakueki wrote:

h = Hash[*(shellwords(source.gsub("="," ")))]

source.gsub(/^([^=]+)=/) {$1}

in case there is another = char on the line.

Nice to know about shellwords for this purpose, thanks!

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Joel VanderWerf wrote:

Harry Kakueki wrote:

h = Hash[*(shellwords(source.gsub("="," ")))]

source.gsub(/^([^=]+)=/) {$1}

sorry, should be:

source.gsub(/^([^=]+)=/) {$1 + " "}

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Joel VanderWerf wrote:

Joel VanderWerf wrote:

Harry Kakueki wrote:

h = Hash[*(shellwords(source.gsub("="," ")))]

source.gsub(/^([^=]+)=/) {$1}

sorry, should be:

source.gsub(/^([^=]+)=/) {$1 + " "}

Hi,
   Using source.gsub(/^([^=]+)=/) {$1 + " "} gives me the below shown
result:

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

Since i am new to programming lang, i dont know how to write a regex.
I am looking for output like this

a = "http://localhost"
b = "User@htmdec.com"

where a & b are variables i am storing these values into. let me know if
i am not clear on this.

Cheers

···

--
Posted via http://www.ruby-forum.com/\.

I was just wondering, should we not refuse to use ^ for the beginning
of a string ( even knowing it is a line )? I personally have abandoned
^ for \A for quite some time now. Is nobody going to follow ;).

Just my 0.03€ ( yeah inflation )

R.

···

On Mon, Aug 10, 2009 at 9:20 PM, Joel VanderWerf<vjoel@path.berkeley.edu> wrote:

Joel VanderWerf wrote:

Harry Kakueki wrote:

h = Hash[*(shellwords(source.gsub("="," ")))]

source.gsub(/^([^=]+)=/) {$1}

sorry, should be:

source.gsub(/^([^=]+)=/) {$1 + " "}

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

--
module Kernel
  alias_method :λ, :lambda
end

Shekar Ls wrote:

Since i am new to programming lang, i dont know how to write a regex.
I am looking for output like this

a = "http://localhost"
b = "User@htmdec.com"

where a & b are variables i am storing these values into.

You would never do that. You store multiple values in arrays. For
instance,

data = ["hello", "hi", "goodbye"]
puts data[1]

--output:--
"hi"

You can get the data out of your file without using regex's like this:

results =

IO.foreach("data.txt") do |line|
  pieces = line.split("=")
  val = pieces[1].strip #remove spaces on the left and the right
  val = val[1...-1] #slice off the quotes
  results << val
end

p results

--output:--
["http://localhost", "User@htmdec.com", "12345678", "iexplore"]

···

--
Posted via http://www.ruby-forum.com/\.

Robert Dober wrote:

···

On Mon, Aug 10, 2009 at 9:20 PM, Joel VanderWerf<vjoel@path.berkeley.edu> wrote:

source.gsub(/^([^=]+)=/) {$1 + " "}

I was just wondering, should we not refuse to use ^ for the beginning
of a string ( even knowing it is a line )? I personally have abandoned
^ for \A for quite some time now. Is nobody going to follow ;).

Didn't the OP want each line converted? It's getting hard to remember... Anyway, I did mean ^ in my answer, but maybe I was confused about the question.

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Robert Dober wrote:

I was just wondering, should we not refuse to use ^ for the beginning
of a string ( even knowing it is a line )? I personally have abandoned
^ for \A for quite some time now. Is nobody going to follow ;).

I didn't follow you, but I've been using \A..\z for years.

This is certainly an area where Ruby can trip you up dangerously, if
you're expecting Perl-like regular expression behaviour.

···

--
Posted via http://www.ruby-forum.com/\.

7stud -- wrote:

Shekar Ls wrote:

Since i am new to programming lang, i dont know how to write a regex.
I am looking for output like this

a = "http://localhost"
b = "User@htmdec.com"

where a & b are variables i am storing these values into.

You would never do that.

Well, I guess that's not true. You can do this:

results = ["hello", "hi", "goodbye"]
a, b, c = results
puts a, b, c

--output:--
hello
hi
goodbye

···

--
Posted via http://www.ruby-forum.com/\.

Sorry my bad. I did not realize from the snippet that you were doing
the whole source in one thing. But I should have, given the *g*sub.
Yes of course you need to use ^.
As a matter of fact my point was not to use it on lines, because ^
matches at the beginning of a *line* while when we are treating lines
there are no lines anymore and we want to match at the beginning of a
*string*, hence \A.

Cheers
Robert

···

2009/8/17 Joel VanderWerf <vjoel@path.berkeley.edu>:

Robert Dober wrote:

On Mon, Aug 10, 2009 at 9:20 PM, Joel VanderWerf<vjoel@path.berkeley.edu> >> wrote:

source.gsub(/^([^=]+)=/) {$1 + " "}

I was just wondering, should we not refuse to use ^ for the beginning
of a string ( even knowing it is a line )? I personally have abandoned
^ for \A for quite some time now. Is nobody going to follow ;).

Didn't the OP want each line converted? It's getting hard to remember...
Anyway, I did mean ^ in my answer, but maybe I was confused about the
question.

7stud -- wrote:

···

7stud -- wrote:

Shekar Ls wrote:

Since i am new to programming lang, i dont know how to write a regex.
I am looking for output like this

a = "http://localhost"
b = "User@htmdec.com"

where a & b are variables i am storing these values into.

You would never do that.

Well, I guess that's not true. You can do this:

results = ["hello", "hi", "goodbye"]
a, b, c = results
puts a, b, c

--output:--
hello
hi
goodbye

-----------------------

HI,
   Let me be more clear over the requirement , my intention to read from
a remote file & store the values into a variable so that i can make use
of these variables for my automation.
let me try reading this array using a File.open if it works!!

--
Posted via http://www.ruby-forum.com/\.