wood burning stoves*
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Languages » Ruby
Bookmark "updating array elements" Watch "updating array elements" New topic
Author

updating array elements

ric stewart
Greenhorn

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 = 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!


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

Joined: Nov 18, 2009
Posts: 4
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: updating array elements
 
Similar Threads
Output Not understand!!!
Arrays.sort(int[], int, int)
Getting value of all the indices of all objects from an arraylist
Java Help... so frustrated!!
index value