sorting - Ruby read a file, create a hash with a key and data, then sort alphabetically -
i have file below, need put hash:
ghithree, line, number defnumber, two, line abcline, number, 1
i need take first 3 characters , turn key , rest of line value.
so when print hash should this:
keys values abc line, number, 1 def number, two, line ghi three, line, number
here i've got, little on place here is:
lines = file.open("homework02.txt").read.split fhash = {} lines.each |line| next if line == "" fhash[line[0..2]] = line[3..-1] end f = file.open("homework02.txt") fhash = {} loop x = f.gets break unless x fhash[x[0..2]] = x[3..-1] end puts fhash f.close
fhash = fhash.to_a.sort.to_h
Comments
Post a Comment