Meaningless Drivel is fun!
The moose likes Ruby and the fly likes updating array elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Languages » Ruby
Bookmark "updating array elements" Watch "updating array elements" New topic

updating array elements

ric stewart

Joined: Nov 18, 2009
Posts: 4
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 =
myFile ="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}"

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!

Everything depends on the key, the whole key, and nothing but the key so help me Codd.
ric stewart

Joined: Nov 18, 2009
Posts: 4
i got it!

myFile =
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}"

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!
I agree. Here's the link:
subject: updating array elements
It's not a secret anymore!