Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I was given a file of size 46KB full of names (around ~5000), in the format as shown below:

"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH"..."DARRELL","ALONSO"

I wonder if my code below is optimal in doing what I want, which is to translate this txt file into an array of names.

array = []
File.readlines('names.txt').each do |l|
  array << l.delete("\"").split(",")
end

array.flatten
share|improve this question

3 Answers 3

up vote 4 down vote accepted

Yes it is, it takes 0.372798 seconds to run on my computer for a 50 MB file, for a 46KB file this runs in the blink of an eye.

Anyway as this is Codereview I suggest an equally fast but higher level version of your code:

def get_names(filename)
    File.read(filename).tr('"', '').split(',')
end
share|improve this answer
    
Thanks caridorc. Curious how you generate the 50MB file? :P –  Chris Yeung 23 hours ago
    
and yea, i think File.read is what I am looking for instead of using File.readlines, because there is just one line –  Chris Yeung 23 hours ago
1  
@ChrisYeung Obvious, by hand, I had 8 hours to spare :P (not really, the code is def rand_string(len) (0...len).map { (65 + rand(26)).chr }.join end def gen_file names = (1..10000).map{|x| '"'+rand_string(x)+'"'}.join(',') File.write("names.txt", names) end –  Caridorc 22 hours ago

You can also do

def get_names(filename)
  File.read(filename).scan(/[a-z\s]+/)
end

In this case, the scan looks for contiguous runs of letters and spaces, and captures those. So instead of removing quotes and splitting on commas, we're capturing stuff that isn't a comma or quote.

Don't know if it's faster, though (haven't tried it). Just presenting an alternative.

share|improve this answer
    
Thanks! Flambino!! –  Chris Yeung 22 hours ago

This may or may not be faster:

def get_names(filename)
    ('",' + File.read(filename) + ',"').split('","')
end

due to the initial insert possibly reallocating.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.