Arrays with csv file

headers = %w[id name dept]
id_arr = [1,2,3]
name_arr = [“sunil”,“anil”,“shiva”]
dept_arr = [“IT”,“IT”,“IT”]

these are the inputs, i want to write these in csv file how can do it

1 Like

The Ruby docs give nice examples of this

https://ruby-doc.org/stdlib-2.6.1/libdoc/csv/rdoc/CSV.html

headers = %w[id name dept]
arr1 = [1,'sunil','IT']
arr2 = [2,'anil','IT']
arr3 = [3,'garu','IT']

CSV.open("data.csv", "wb") do |csv|
  csv << headers
  csv << arr1
  csv << arr2
  csv << arr3
end
1 Like

headers = %w[id name dept]
id_arr = [1,2,3]
name_arr = [“sunil”,“anil”,“shiva”]
dept_arr = [“IT”,“IT”,“IT”]

sorry, this is my actual problem

So what’s the problem? Are you having problems building the final array from id_arr, name_arr, dept_arr?

in csv file i need the output in below formate
id name dept
1 sunil IT
2 anil IT
3 shivu IT

using the below arrays
headers = %w[id name dept]
id_arr = [1,2,3]
name_arr = [“sunil”,“anil”,“shiva”]
dept_arr = [“IT”,“IT”,“IT”]

Again! What’s the problem? You were given the solution to writing the CSV file. Are you having problems collecting the data from the separate arrays?

yes i am having the problem with collecting the data from the separate arraays

Here you go…

id_arr = [1,2,3]
name_arr = ['sunil','anil','shiva']
dept_arr = ['IT','IT','IT']

arr = [id_arr, name_arr, dept_arr]

final_arr = arr.transpose
p final_arr

okay thank you i will check it

Thank you much its solved my big problem

Thank you much its solved my big problem

Thanks, really a nice solution about problem handling in CSV file. I solved my issue in perticular csv file.

I have a table with several ARRAY columns. I’ve tried loading this table using JSON files, but I get this error:

SQL compilation error: JSON/XML/AVRO file format can produce one and only one column of type variant or object or array. Use CSV file format if you want to load more than one column.

How does the provided links relate to Arrays and CSV files?

Have you tried what @G4143 said? It doesn’t have to do anything with SQL. Please explain your problem.