"Dan Doel" <djd15 / po.cwru.edu> schrieb im Newsbeitrag
news:3FBC87DA.1090803 / po.cwru.edu...
> I'm sure there are many ways of doing this, but here's one:
>
> lst = ['alfred', 'boris', 'bruce', 'claire', 'dean', 'donald']
>
> frob = lst.map do |val|
>     ["-#{val[0..0]}-", val]
> end.flatten.uniq
>
> p frob
>
> I'm not sure if #uniq is defined to leave the first occurrence and
> delete all the rest or not,
> but that's what it currently does, so it works.

There must be an inject way of doing this...

lst = ['alfred', 'boris', 'bruce', 'claire', 'dean', 'donald']

frob = lst.inject( [[], nil] ) do |(res, key), word|
  k = "-#{word[0].chr}-"
  res << k unless k == key
  res << word
  [res, k]
end[0]


But this really cries for a Hash, because we have a typical key value
relationship here:

lst = ['alfred', 'boris', 'bruce', 'claire', 'dean', 'donald']

lst.inject( Hash.new {|h,k| h[k]=[]} ) {|res,w| res["-#{w[0].chr}-"] << w;
res}.sort.flatten

In fact, I'd just return the Hash.  Then you have the appropriate data
structur.  Simply omit ".sort.flatten" from the line above.

Regards

    robert