|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Reading Textfiles
Hi,
I got a textfile of integers like the one below. 1 2 3 323 4656 22 3 0 When I use f = File.open("file.txt","r") list = f.readlines My array list is populated with strings, that is list = ["1","3"....etc] rather than integers. How do I read the textfile and put actual integers to my array wit elements of the textfile? |
|
#2
|
|||
|
|||
|
You could use map:
Code:
data = File.open(file).readlines.map { |l| l.chomp.to_i }
Of course, you will want to some verification of that input to make sure that you are not trying to convert a word into a numeric value...
__________________
Some people have 20 years of experience. Some have 1 year of experience 20 times. My personal site: Basic geek randomness |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Ruby Programming > Reading Textfiles |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|