I wrote a ruby script hash sort related, but feel it's not good, is it
possible to simplify?

==========================================
There is a string("string_sample", every line end with "\n"),
I want the strings sort by user names with the licenses number used.(If
line without license number, then license number=1)
example: User name1 use 1(string_sample line1) + 2(string_sample line4)
, total 3 licenses
==========================================
string_sample = "    name1 machin1 NoXDisplay (v2012.12) (server1/port1
18709), start Fri 1/24 17:26
    name2 mchine2 NoXDisplay J3_ffffffffa8c0a600_21848_2 (v2012.12)
(server2/port2 13335), start Fri 1/24 9:29
    name2 machine3 NoXDisplay J3_ffffffffa8c0a600_21848_1 (v2012.12)
(server3/port3 8569), start Fri 1/24 9:26
    name1 machine4 NoXDisplay (v2012.12) (192.168.0.197/27007 38446),
start Fri 1/24 19:07, 2 licenses
    name3 machine5 NoXDisplay (v2012.12) (server4/port4 10195), start
Wed 1/22 10:30
    name3 machine6 NoXDisplay J3_ffffffffa8c0a600_21848_3 (v2012.12)
(server5/port5 6975), start Fri 1/24 9:30
    name6 machine7 NoXDisplay (v2012.12) (server6/port6 16114), start
Thu 1/23 15:31"

=>

==================
name1(3)
==================
    name1 machin1 NoXDisplay (v2012.12) (server1/port1 18709), start Fri
1/24 17:26
    name1 machine4 NoXDisplay (v2012.12) (192.168.0.197/27007 38446),
start Fri 1/24 19:07, 2 licenses
==================
name3(2)
==================
    name3 machine5 NoXDisplay (v2012.12) (server4/port4 10195), start
Wed 1/22 10:30
    name3 machine6 NoXDisplay J3_ffffffffa8c0a600_21848_3 (v2012.12)
(server5/port5 6975), start Fri 1/24 9:30
==================
name2(2)
==================
    name2 mchine2 NoXDisplay J3_ffffffffa8c0a600_21848_2 (v2012.12)
(server2/port2 13335), start Fri 1/24 9:29
    name2 machine3 NoXDisplay J3_ffffffffa8c0a600_21848_1 (v2012.12)
(server3/port3 8569), start Fri 1/24 9:26
==================
name6(1)
==================
    name6 machine7 NoXDisplay (v2012.12) (server6/port6 16114), start
Thu 1/23 15:31



#==========================================
#test.rb
#==========================================
string_sample = "    name1 machin1 NoXDisplay (v2012.12) (server1/port1
18709), start Fri 1/24 17:26
    name2 mchine2 NoXDisplay J3_ffffffffa8c0a600_21848_2 (v2012.12)
(server2/port2 13335), start Fri 1/24 9:29
    name2 machine3 NoXDisplay J3_ffffffffa8c0a600_21848_1 (v2012.12)
(server3/port3 8569), start Fri 1/24 9:26
    name1 machine4 NoXDisplay (v2012.12) (192.168.0.197/27007 38446),
start Fri 1/24 19:07, 2 licenses
    name3 machine5 NoXDisplay (v2012.12) (server4/port4 10195), start
Wed 1/22 10:30
    name3 machine6 NoXDisplay J3_ffffffffa8c0a600_21848_3 (v2012.12)
(server5/port5 6975), start Fri 1/24 9:30
    name6 machine7 NoXDisplay (v2012.12) (server6/port6 16114), start
Thu 1/23 15:31"


def check_license_number_each_line(lmstat_line)
             lic_used_num_eachline = 0
             if lmstat_line =~ /(\d+)\s+licenses/
                lic_used_num_eachline = $1.to_i
             else
                lic_used_num_eachline = 1
             end
             return lic_used_num_eachline
end

hash_save = Hash.new
string_sample.lines.each do |x|
        name = x.split(/\s+/)[1]
        if hash_save.include?(name)
                hash_save[name] += x
        else
                hash_save[name] = x
        end
end

hash_save.each {|key, value|
        account_lic_used = 0
        value.each_line {|line|
                account_lic_used += check_license_number_each_line(line)
        }
        hash_save[key] = [value, account_lic_used]
}

a = hash_save.sort_by {|key, value| value[1]}.reverse

puts '**********************'
puts 'Sort by license used:'
puts '**********************'
a.each {|key, value|
        puts '=================='
        puts "#{key}(#{value[1]})"
        puts '=================='
        puts value[0]
}

-- 
Posted via http://www.ruby-forum.com/.