From: Michael Neumann Date: 2001-10-09T21:44:37+09:00 Subject: [ruby-talk:22302] Re: What's wrong with these Arrays? Jens Rohde wrote: > Hi ruby-hackers > > I'm working on a script that are supposed to analyse the logfiles from my > cisco PIX firewall. > > One of the tasks is to find the first and the last date a given ip-address > has done a violation. For that I've produced the following code: > > theIP.each do |plof| # Traverse the list with violating IP's > sql = "select date from pixlog where srcIP = \"#{plof}\" order by date" > getData = @dbHandle.prepare(sql); getData.execute > tempArray = Array.new > while tempVar = getData.fetch do > tempArray.push(tempVar) replace above line with: tempArray.push(tempVar.dup) > end > firstDate = tempArray[0].to_s > lastDate = tempArray.last.to_s > @report.push("From "+firstDate+" to "+lastDate+" "+plof+" ... > end > > For some reason tempArray alwas seems to be filled with the last value > delivered by the getData-fetch command. It seems that you use Ruby/DBI for DB access. StatementHandle#fetch (and all the other similar methods) return a reference to an internal array where the columns of each requested row are stored in => DBI only creates one Array instead of one for each row. If you want to store one row completely elsewhere you have to duplicate it using method "dup"; if you just use some fields of a row, you don't have to do so. I'd rewrite you're piece of code as follows: sql = "select date from pixlog where srcIP = ? order by date" getData = @dbHandle.prepare(sql) theIP.each do |plof| # Traverse the list with violating IP's getData.execute(plof) dates = getData.collect {|row| row['date']} # store only the dates not the whole rows @report.push("From "+dates.first+" to "+dates.last+" "+plof+" ... end And if you're database supports "fetch_scroll", I'd use this instead for increasing performance. Regards, Michael -- Michael Neumann merlin.zwo InfoDesign GmbH http://www.merlin-zwo.de