Hello,
Thanks for the snippet. IT works.
But I guess I should have posed my question differently. The new snippet and
the first snippet works great to get every value (from second column) from
every row  of the CSV file and inserting into an Array.

PLease see the attached CSV file.
My problem is: How do I start grabbing data from a random row:  say row 11
(column 2) and store in an array, instead of starting from the beginning of
the CSV file?
Or in other words, Is there a way to specify a particular row to start
collecting data using the CSV module?

I hope my question is clear.
Thank you.

Margam

On Tue, Jan 13, 2009 at 12:32 PM, Bill Agee <billa...@gmail.com> wrote:

>
> Hey,
>
> One way to do this is to use a counter while iterating over the rows:
>
> require 'csv'
> array = []
> row_count = 0
> CSV::Reader.parse(File.open('foo.csv')) do |row|
>  row_count += 1
>  # Don't save the value on the first row
>  next if (row_count == 1)
>  array << row[1]
> end
>
> That code will increment row_count by 1 at the beginning of each
> iteration performed.  So as shown, you can use the counter to exit the
> block early if you are on row 1.
>
> For more info, I'd suggest checking out the Ruby programming resources
> on the Wiki, at:
>
> http://wiki.openqa.org/display/WTR/Learning+Ruby
>
> The Ruby Cheat Sheet document there contains a brief introduction to
> iteration, and the other docs go into more depth.
>
>
>
> On Tue, Jan 13, 2009 at 11:48 AM, Margam <nk.mar...@gmail.com> wrote:
> >
> > Hello everyone,
> > Can anyone help with my previous question.
> > So I  am trying to get data starting from a particular row (instead
> > of from the first row) of a CSV file and have not been very
> > successful.
> > Say I want to start getting data from Row 2 and as before Column 2 of
> > my CSV file.
> > I tried the following, but did not work:
> >
> > array=[]
> > CSV::Reader.parse(File.open('gmail.csv')) { |row| array << [1][1] }
> > CSV::Reader.parse(File.open('gmail.csv')) { |row| array << ['r1c1'] }
> >
> > Can anyone help me? How should I be specifying the row and columns to
> > be
> > saved in the array?
> > Thank you very much.
> > Margam
> >
> > On Jan 11, 5:28 pm, Margam <nk.mar...@gmail.com> wrote:
> >> Hello Bill,
> >> So I  was trying to get data starting from a particular row (instead
> >> of from the first row) and have not been very successful.
> >> Say I want to start getting data from Row 2 and as before Column 2.
> >> I tried the following, but did not
> work:CSV::Reader.parse(File.open('gmail.csv')) { |row| array << [1][1]
> }CSV::Reader.parse(File.open('gmail.csv')) { |row| array << ['r1c1'] }
> >>
> >> Can you help me? How should I be specifying the row and columns to be
> >> saved in the array?
> >> Thank you very much.
> >>
> >> Margam
> >>
> >> On Jan 8, 3:16 pm, "Bill Agee" <billa...@gmail.com> wrote:
> >>
> >> > Thecsvmodule should be able to do what you need, without much fuss:
> >>
> >> > require 'csv'
> >> > array = []CSV::Reader.parse(File.open('foo.csv')) { |row| array <<
> row[1] }
> >>
> >> > That snippet will open 'foo.csv', and for each row, push the second
> >> > column's value onto the array.  Note that if you need to deal with
> >> > lines that are commented, empty, or otherwise invalid, a little more
> >> > work would be needed to handle those situations.
> >>
> >> > More info:
> >>
> >> >http://www.ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html(Clickon<http://www.ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html%28Clickon>
> >> > theCSV::Reader class for applicable docs)
> >>
> >> > On Thu, Jan 8, 2009 at 2:48 PM, Margam <nk.mar...@gmail.com> wrote:
> >>
> >> > > Hello all,
> >> > > I am trying to get data from a particular column (second in my case)
> >> > > of anCSVfile and store it into an array. And then use the array
> >> > > elements within my script
> >> > > Curerntly my script(below) uses the "datahandler.rb"
> >> > > ---------------
> >> > > require 'watir'
> >> > > require 'watir/datahandler'
> >> > > require "test/unit"
> >>
> >> > > #Get Data from theCSVfile and store in variables.CSVfile is in the
> >> > > same folder as the script
> >> > > d = DataHandler.new("gmail.csv")
> >>
> >> > > $dataarray=[] #Creating an empty array
> >>
> >> > > #storing each value from theCSVfile to the array elements. Note:
> >> > > "Value" is the header of the second column in theCSVfile.
> >> > > $dataarray[0]=d.data[0].Value
> >> > > $dataarray[1]=d.data[1].Value
> >> > > $dataarray[2]=d.data[2].Value
> >> > > $dataarray[3]=d.data[3].Value
> >> > > ...
> >>
> >> > > #rest of script, where the array element are used.
> >> > > -------------------
> >> > > The above approach works. But I don't want to manually enter each
> >> > > value from the file into each array element.
> >> > > Is there an easier and better (shorter) way to achieve this?
> >>
> >> > > Please help.
> >> > > 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
-~----------~----~----~----~------~----~------~--~---

Attachment: gmail.csv
Description: MS-Excel spreadsheet

Reply via email to