From: Tobias Reif Date: 2002-03-12T07:36:36+09:00 Subject: Re: Using 'uniq' question Ed Baker wrote: > If I have an array of data, where each record looks like the example > below the cut line, how could I do a "uniq" sort based only on the > first column. > > ===========================cut line ======================== > 100788 1000 1538 > 100791 1000 1505 > 100791 1000 1520 > 100792 1000 1698 Here's a probably inefficient solution (does it do what you want?) ----- class Array def any? bool_ary = [] each do |el| bool_ary.push(yield(el)) end bool_ary.delete false bool_ary.compact! not bool_ary.empty? end end #p([3,1,5].any? {|el| el == 1}) #=> true #p([3,1,5].any? {|el| el == 4}) #=> false # #p(['foozboozer','hubbabubba'].any? {|el| el =~ /oo/}) #=> true #p(['foozboozer','hubbabubba'].any? {|el| el =~ /\d+/}) #=> false records = '100827 1000 1516 100792 1000 1698 100792 1000 1698' record_lines = records.split "\n" unique_records = [] record_lines.select do |line| first_n = line.split()[0] unless unique_records.any? {|el| el.split()[0] == first_n} unique_records.push line end end sorted_records = unique_records.sort do |a,b| a.split()[0].to_i <=> b.split()[0].to_i end puts sorted_records # 100792 1000 1698 # 100827 1000 1516 ----- Tobi -- * peace&love. * http://www.pinkjuice.com/