From: MonkeeSage Date: 2007-12-30T10:00:00+09:00 Subject: Re: using reg expr with array.index On Dec 27, 7:17 pm, Esmail wrote: > MonkeeSagewrote: > > > def no_dups(path) > > IO.read(path).split(" >").uniq.join(" >") > > end > > fixed = no_dups("testfile") > > puts fixed > > One more quick questions (ha .. see, that's what you get > for being so helpful) - please feel free to ignore this. No problem. What is really cool about the ruby community is that everyone is willing to help (and to learn!). I've never found a better programming community. So don't feel like any question is dumb or that you're asking too much. :) > For the above solution which I really like, is there an > easy way to get the duplicate records? (I'd like to display > the name lines ie the ones that start with > as a possible > check of what I am eliminating from the original data). > > I know how to do this if I reread the file again and traverse > it but that's certainly not an efficient way to do this. > > I am doing a lot of reading on Ruby right now, so I'll > may come across the solution, so only reply if you are bored :-) > > (ps: I suppose if there was a to_set and to_array functionality > in Ruby - for all I know there is - it would have yet provided > another approach to solve the original problem) Without making assumptions about ordering and such, I'm not sure it's possible to avoid multiple iterations (and probably polynomial time) if you want to roll your own #uniq method to return an array of (or otherwise process) duplicate elements. Off the cuff, I'd say that something like this is probably the most efficient (but please correct if there's a better way): def no_dups(path) seen = [] dups = [] IO.read(path).split(">").each { | item | if seen.include?(item) dups << item # or, for example... # puts %{Removed dup: >#{item.split("\n")[0]}} else seen << item end } [seen.join(">"), dups] end fixed, dups = no_dups("testfile") Ps. I think google is indenting the ">" because it thinks it's the start of a quote. Regards, Jordan