From: "Jan E." Date: 2012-05-02T18:55:50+09:00 Subject: Re: array of strings - finding letter combinations Hi, Sebastjan H. wrote in post #1059172: > > I am new to ruby and I want to learn by examples and here is one from my > everyday work. I have an array of strings where each string contains a > combination of an ampersand and a letter (&e). The trick is, this > combination may only appear once in the whole array, so none of the > other strings may contain the same combination. You could start off by selecting the letter after "&" in a string. This can be done with a regex: str = 'abc&exyz' str[/(?<=&)[a-z]/] Then you iterate over the array, collecting the duplicate strings and the used characters: strings = ['&a', '&b', '&c', '&a'] used_chars = [] duplicates = [] strings.each_with_index do |str, i| char = str[/(?<=&)[a-z]/] duplicates << i if used_chars.include? char used_chars << char end And after this you iterate over the duplicate strings and suggest and unused character (or raise in error, if there are none left): duplicates.each do |index| unused = ('a'..'z').find {|char| not used_chars.include? char} if unused.nil? raise 'all letters taken' else puts "combination #{strings[index][/&[a-z]/]} already taken.", "Use &#{unused} instead?" end end The actual processing of the user input has to added, of course. -- Posted via http://www.ruby-forum.com/.