Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

updating array elements

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm a ruby nube and i'm trying to build an array of php array functions from a csv file; for each element in the array i would like to enclose each with double quotes; here's my code:

myFile = Array.new
myFile = File.open("C:/my_ruby_code/list_of_php_array_functions.txt")
myFile.each_with_index do |line, index|
line = line.strip
concatenated_line = '"'+line+'"'
#myFile[index] = concatenated_line #<----this is the offending line! for some reason it won't let me reassign a value for this slot!!
puts "#{index} array item: #{line}"
puts "#{index} array item: #{concatenated_line}"
end

OUTPUT, this is the output when i leave the # comment on the line above:
0 array item: array_change_key_case
0 array item: "array_change_key_case"
1 array item: array_chunk
1 array item: "array_chunk"
2 array item: array_combine
2 array item: "array_combine"
3 array item: array_count_values
3 array item: "array_count_values"
4 array item: array_diff_assoc
4 array item: "array_diff_assoc"
5 array item: array_ diff_key
5 array item: "array_ diff_key"

Your help is much appreciated. Thanks all!
 
ric stewart
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got it!

myFile = Array.new
myFile =File.readlines("C:/my_ruby_code/list_of_php_array_functions.txt")
myFile.collect! {|element| element.strip}
myFile.collect! {|element| '"'+element+'"'}
myFile.each_with_index do |element, index|
puts "#{index} array item: #{element}"
end

i started to create a 'procedual' bunch of code (me being an old mantis programmer) until i stumbled across the collect method, ruby is so sweet!
 
The moustache of a titan! The ad of a flea:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic