philsivyer
asked on
def - sort_by
Can someone tell me what is wrong with the sort_by? - see "def printfilelinedetails"
Trying to sort by column 3 - but not working.
Regards
Phil
Trying to sort by column 3 - but not working.
Regards
Phil
require "csv"
class CSVtest
def initialize(filename)
@filename = filename
@results = CSV.read(@filename)
end
def printfilelinedetails
@results = File.open(@filename)
@results.each do |line|
sorted = line.sort_by{|w|w[2]}
puts sorted
end
end
def printReferenceNos
refnos = @results.find_all do |r|
r[1]=="Ref0001" or r[1]=="Ref0002"
end
puts "#{Time.now}\nFollowing reference numbers were selected:"
refnos.each {|m| puts "#{m[1]},#{m[2].downcase}"}
end
def sumcol3
puts "the total of Man Hours Lost is: #{ @results.inject(0){|sum, line| sum + line[3].to_f}.to_s}"
end
def selectVectusorSAP
@results = File.open(@filename)
@results.each do |line|
if line =~ /(vectus)|(sap)/i then
line
puts line
end
end
end
phil = CSVtest.new("C:/RUBY_WORKING_MODELS/csv_master.txt")
phil.printfilelinedetails
puts "\n"
phil.selectVectusorSAP
puts "\n"
phil = CSVtest.new("C:/RUBY_WORKING_MODELS/csv_master.txt")
phil.printReferenceNos
puts "\n"
phil.sumcol3
end
ASKER
Hello
Thanks for the response - still strugling with the sort_by logic. Can I try a new approach - assume my file is as follows:
COL_A, COL_B,COL_C
A, 2, SS
C, 1, XX
B, 24, 123
what is the code for sorting by Col_b then Col_a then Colc?
Regards
Thanks for the response - still strugling with the sort_by logic. Can I try a new approach - assume my file is as follows:
COL_A, COL_B,COL_C
A, 2, SS
C, 1, XX
B, 24, 123
what is the code for sorting by Col_b then Col_a then Colc?
Regards
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
cminear
Thanks for this - works a treat
Regards
Phil
Thanks for this - works a treat
Regards
Phil
ASKER
Good work - thanks for prompt reply
You also say you want to sort by _column_ 3; the current code (opening the file with File.open) would sort by the 3rd character column, not the 3rd CSV column. Furthermore, you are essentially overwriting the @results instance variable that was set during initialization, which you may not want to do.
I believe the code snippet below contains a corrected 'printfilelinedetails' method. Note that the sorting of the results is immediately passed to 'each' to be outputted, and that we "rebuild" the line. (If you really want the line as it originally is in the file, and hence use the 'File.open', then you need to perform the splitting yourself in the 'sort_by' method call.) 'sort_by' does not alter the contents of the @results variable, meaning @results if used again is still unsorted.
Open in new window