Hi Alex,
Thank you very much for the explanation.

Margam

On Tue, Jan 13, 2009 at 11:39 PM, Alex Collins <a.j.collins...@gmail.com>wrote:

>
> My apologies - knocked send whilst getting onto the train.
>
> In this case, you are using the CSV modules to load strings as key/
> value pairs into a hash. The important bit being that both the key,
> and the value when retrieved from the hash will be strings.
>
> Let us examines few snippets of your code:
>
> hash[URL]
>
> This tries to access the value in the hash stored for the key
> contained in the URL constant. In Ruby, If is starts with a capital,
> it is a constant. As you haven't initialised this constant, Ruby does
> not know which element to look at. Hence the error.
>
> So the quotation marks are required because your key is a string.
>
> And alternative would be to use a symbol eg:
>
> hash[row[0].to_sym] = row[1]
>
> You could then access it with:
>
> hash[:URL]
>
> (warning: I'm not certain about capitals as symbols - may need to be
> lower case)
>
> Likewise, when you access the value, you are accessing a String object
> (an instance of a Ruby class). That it is a string is indicated by the
> double quotes.
>
> You may be interested in looking at YAML. It is a markup form
> available in Ruby which is typically used for what you are doing.
>
> Hope this helps,
>
> Alex
>
> On 14 Jan 2009, at 01:19, Margam <nk.mar...@gmail.com> wrote:
>
> >
> > Follow Up
> > Hello All,
> > After googling for some time, I found an easy way to get the data into
> > an Hash. Thanks to One Mr.Ryan.
> > ---------------------
> > hash = {}
> > CSV.foreach('test.csv') do |row|
> >  hash[row[0]] = row[1]
> > end
> > -----------------
> >
> > But I have some questions regarding accessing the values using the Key
> > field.
> > Based on the CSV file example I had mentioned in my previous entry:
> > 1. When I do:
> > $browser.goto(hash[URL])
> > I get a error:
> > --------------
> >    NameError: uninitialized constant TestSuite::URL
> >    c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.2/lib/
> > active_support/dependencies.rb:493:in `const_missing'
> >    csvdriven.rb:102:in `test00_gmail'
> > --------------
> > But works fine when I do: $browser.goto(hash['URL']).
> > Why are the single quotes required?
> >
> > 2. When I try to output the values using the KEys like this:
> > puts hash['URL']
> > the output looks like this:  "http://gamil.com";
> > Why does the output has the double quotes?
> >
> > Hope my questions are clear. Thank you.
> >
> > Margam
> >
> > On Jan 13, 3:58 pm, Margam <nk.mar...@gmail.com> wrote:
> >> Hello All,
> >> I have been recently learning to use both the CSV module and the
> >> FasterCSV gem to create an array from a CSV file.
> >> Say my CSV file(file.csv) looks like this (first row is the header,
> >> not used in script):
> >> Parameter,Value
> >> URL,http://gamil.com
> >> Login,mylogin
> >> Password,mypassword
> >>
> >> My script using the CSV module is:
> >> require 'csv'
> >> array=[]
> >> CSV::Reader.parse(File.open("file.csv")){|row| array << row[1]}
> >>
> >> The above code saves the Values in column two into an array. I use
> >> the
> >> array elements in my script.
> >>
> >> IN the same way, is there a method to create a Hash table. I would
> >> like to access the values in column 2 using the Key on column 1. I
> >> want to do this, as the CSV file may keep changing.
> >> Also how to access the Hash keys(or use them in the script)?
> >>
> >> Kindly provide guidance.
> >> Thank you
> >>
> >> Margam
> > >
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Watir General" group.
To post to this group, send email to watir-general@googlegroups.com
Before posting, please read the following guidelines: 
http://wiki.openqa.org/display/WTR/Support
To unsubscribe from this group, send email to 
watir-general-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/watir-general
-~----------~----~----~----~------~----~------~--~---

Reply via email to