Transfer content of flat file to an array

S

Surjit Nameirakpam

Scenario ..

I have a text file

23
24
56
78
90
89

I want to transfer this content into an array,
numArr=[23,24,56,78,90,89]

How to do it
 
A

Alex Young

Surjit said:
Scenario ..

I have a text file

23
24
56
78
90
89

I want to transfer this content into an array,
numArr=[23,24,56,78,90,89]

How to do it
File.readlines, map, to_i.
 
E

Eustáquio 'TaQ' Rangel

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I have a text file
23
24
56
78
90
89
I want to transfer this content into an array,
numArr=[23,24,56,78,90,89]
File.readlines("surjit.txt").map {|item| item.chomp}
=> ["23", "24", "56", "78", "90", "89"]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFHO1iTb6UiZnhJiLsRApK1AKCNW3uah81gxjhPRfbycRpidF9ORwCeMY6z
Z8ZmbQqMMG2vUAfkRSzvDb0=
=pKJi
-----END PGP SIGNATURE-----
 
P

Phrogz

I have a text file

23
24
56
78
90
89

I want to transfer this content into an array,
numArr=[23,24,56,78,90,89]

IO.read( 'myfile.txt' ).scan( /\d+/ ).map{ |s| s.to_i }

....assuming you don't have any floating point numbers in there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top