0

Here is the list of item in my array: If you see there are files generated with the dates on them (e.g., update.20130410.xml). I should be taking only the latest file and remove the rest. Let's assume that the array values are sorted, so it should be fine if we are just picking the top item among the repeating pattern. Please help me.

Original array:

theOneBig.xml
theSecondFine.xml
theTestOne.xml
OtherImp.xml
mrss.xml
update.20130410.xml
update.20130402.xml
update.20120314.xml
update.20120313.xml
update.20120312.xml
update.20120301.xml
update.20130411.xml
file.update.20130410.xml
file.update.20130409.xml
file.update.20130409.xml

Output I would like to get is

theOneBig.xml
theSecondFine.xml
theTestOne.xml
OtherImp.xml
mrss.xml
update.20130410.xml
file.update.20130410.xml
2
  • 2
    What is "the latest file"? What would the output be when the duplicates are removed? Commented Apr 11, 2013 at 14:47
  • I updated the post, how i wanted my final result Commented Apr 11, 2013 at 15:06

1 Answer 1

1

Assuming that the list is already sorted properly, the dates always begin and end with periods, and that you never have a sequence of digits beginning and ending with periods that isn't a date, then this should work (for Ruby 1.9.3):

original_array.uniq {|f| f.gsub(/(?<=\.)\d+(?=\.)/, '') }

When you provide a block to Array#uniq, it uses the return value to determine "uniqueness".

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Wally for your answer, but I am getting SyntaxError: compile error (irb):20: undefined (?...) sequence: /(?<=\.)\d+(?=\.)/
It works with Ruby 1.9.3, but I got the same error as you using 1.8.7.
1.8.7 regexes don't have look ahead/behind

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.