Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

updating array elements

 
ric stewart
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic