I am new to Ruby. I am trying to read an excel (xls) filr using the
'Parseexcel' gem. My excel file has 2 columns and 5 rows, i.e, i have
data in 10 cells.
I have written the following code. I am getting all the values from the
cell and then assign it to my 'temp' array. But when i see the array
length it comes out to be '2'. Also when i print this temp array
#cycle over every row
temp =
worksheet.each do |row|
row.each do |cell|
temp = cell.to_s('latin1')
Here you are setting the 'temp' var to refer to the string
You need to add the string to the array
I think temp.append() or the '<<' operator is what you need
cheers
···
On Wed, Oct 24, 2012 at 2:06 PM, Rochit Sen <lists@ruby-forum.com> wrote:
Again: what you are doing here is reassign the temp variable in
every iteration (so it is always set to only the last cell content,
previous assignments are irrelevant).
What you want is to append elements to an existing array -> push
Consider
1.9.3-p194 :001 > temp =
1.9.3-p194 :002 > [1,2,3].each do |element|
1.9.3-p194 :003 > temp = element # reassigns temp to some number
1.9.3-p194 :004?> p temp
1.9.3-p194 :005?> end
1
2
3
1.9.3-p194 :006 > p temp
3
as opposed to
1.9.3-p194 :001 > temp =
1.9.3-p194 :002 > [1,2,3].each do |element|
1.9.3-p194 :003 > temp << element # appends the number to temp array
1.9.3-p194 :004?> end
1.9.3-p194 :005 > p temp
[1, 2, 3]
···
Am 25.10.2012 06:42, schrieb Rochit Sen:
Joel Pearson wrote in post #1080994:
temp.push cell
Thanks @Joel and @Chris
I used the push method and got it working. But was wondering one thing,
that i can assign values in an array by writing the following:
Arr = [1,2,3]
but i cannot use var that is being iterated by a "do..end" and assign
the values of var into an array as i was doing earlier:
1.9.3-p194 :001 > temp =
1.9.3-p194 :002 > [1,2,3].each do |element|
1.9.3-p194 :003 > temp << element # appends the number to temp array
1.9.3-p194 :004?> end
1.9.3-p194 :005 > p temp
[1, 2, 3]