1

pretty simple issue but can't seem to figure it out. Checked for answers before coming here, nothing too informative yet.

Anyways, I have roughly 4000 rows in excel. Column A, and Column B.

What would be the best way to make an array and be able to store that?

1

1 Answer 1

2

Please check the following code,

require 'spreadsheet'
a=Array.new
b=Array.new
Spreadsheet.open('abc.xls') do |book|
  book.worksheet('Sheet1').each do |row|        
    a.push(row[0])
    b.push(row[1])
  end
end

This Will return two array a, b. Please check it

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Shamith c. Well, it didn't fail or return an error, but when I run the ruby file in terminal, it doesn't 'do' anything, just starts a new $ line in the terminal. Is that what is suppose to happen? I mean using irb/rails c isn't going to accomplish anything here is it? B/c whatever you do in there is just...there? Unless you save it. Sorry for my noobness.
Alright I added book.write '/Users/davidpardy/caweek4/rubytests/newarrayfile2.txt' after the push and it is making the file in the rubytests folder (just did a .txt file for now) and there is all these symbols. For example !"#$%&'()*+,-./0123456789:;<=>?@AC˝ˇˇˇ I also added Spreadsheet.client_encoding = 'UTF-8'
If you want to see array. Just add two line at end of the code puts a puts b
Hm, after adding push a and push b the array is showing up in terminal when I run it, but there are some duplications. For example, the first number in Column A, row 1 is -81.200399. This number shows up numerous times when the array is run in terminal. Also, the pattern through my excel sheet is a -81.xxxx for column A, and a 28.xxxx for column B. In terminal, there are many areas where there will be a bunch of 28.xxxx numbers in a row, and sometimes lots of -81.xxxx numbers in a row. Why would this be?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.