From: "Jesús Gabriel y Galán" Date: 2010-04-10T01:56:36+09:00 Subject: Re: read and write stock prices to file using arrays On Fri, Apr 9, 2010 at 3:57 PM, Peter Miller wrote: >> An idea would be to fill a hash with the data retrieved for each symbol: >> >> ticker_data = {} >> >> YahooFinance.get_quotes( quote_type, quote_symbols ) do |qt| >>   puts "QUOTING: #{qt.symbol}" >>   puts "#{qt.name}" >>   puts "#{qt.lastTrade}" >>   ticker_data[qt.symbol] = [qt.name, qt.lastTrade] >> end > > I didn't realize a hash could point to more than one object. Awesome. As Martin explained, it's just one object. It happens to be an array with many other objects inside, or it could be a struct, for example: Quote = Struct.new(:symbol, :name, :last_trade) YahooFinance.get_quotes( quote_type, quote_symbols ) do |qt| ticker_data[qt.symbol] = Quote.new qt.symbol, qt.name, qt.last_trade end or another hash: YahooFinance.get_quotes( quote_type, quote_symbols ) do |qt| ticker_data[qt.symbol] = {:symbol => qt.symbol, :name => qt.name, :last_trade => qt.last_trade} end depends on how you want to use it later. > >> and then write to the file the data in ticker_data. I can see >> additional complexity if you want to keep the file exactly as it is >> (with the comments) but inserting the retrieved data in place. The >> easiest would be to just write ticker_data: > > this is why i was trying to do $ticker[lines] = line when storing the > data. I thought I could point the index of the ticker symbol, which > would tell me what line it was on, to my array $ticker. Then i wanted to > use that index to write back the ticker data to the same line it came > from. The problem is that you can't really insert things "in the middle" of a file. If you write something in the current position of a file (the cursor), it will overwrite what's after, it doesn't push the rest of the file to make space for what you are writing. So, the usual simple way is to read the file, modify it in memory and write it all again. If you want to preserve everything, you could split the file in chunks with the comments, and replace the other lines with the result of your calculation. For that, you will have to keep track of which lines represent ticker data. For example (untested): TickerData = Struct.new :symbol, :file_position, :name, :last_trade lines = File.readlines(filename) tickers = {} lines.each_with_index do |line, i| next if line =~ /^#/ # skip comments next if line.chomp.empty? # skip empty lines symbol = line[/\w+/] tickers[symbol] = TickerData.new symbol, i #store the symbol and the file position end YahooFinance.get_quotes( quote_type, tickers.keys ) do |qt| next unless tickers[qt.symbol] tickers[qt.symbol].name = qt.name tickers[qt.symbol].last_trade = qt.last_trade end Now you have the original lines in an array and a hash with all the data, including to which line it corresponds, you can sub the line with what you want, and then write the file back: tickers.each do |symbol, ticker| lines[ticker.file_position] = "#{ticker.symbol} #{ticker.name} #{ticker.last_trade}\n" end File.open(filename, "w") {|file| file.puts lines.join} I think this should work but I haven't tested it. Jesus.