Final iteration lost?

Leam Hall <[email protected]> Thu, 8 Aug 2019 07:07:31 -0400
Newsgroups gmane.comp.lang.ruby.general
Message-ID <[email protected]>
I'm parsing a data file with each_line, each section starts with a line 
that ends in ")". Each section has 2-3 subsections, delimited by blank 
lines. The original test for a new section was:

	if line.end_with?(')')

However, that means the last section is never dealt with, since the 
"each_line" ends at EOF. What I currently have is a line to process the 
data once the each_line block is finished.

	if data.count > 0
	  jobs << Job.new(data)
	end

Full code below.

Thanks!

Leam

### Sample job section

Linux Admin, Spidernet s.r.l, Repubblica Italiana, (1997 - 1998)

Supported Linux and Solaris in a multi-lingual environment.  Configured 
Apache to support application development team's needs. Took ownership 
of OS and hardware production issues and channeled application issues to 
specialists.

Solaris 2.5, Red Hat Linux 3, 4, 5, 6, Sendmail, Apache, Tomcat



### parser

$VERBOSE = true

$LOAD_PATH << File.expand_path('../../lib/', __FILE__)
require 'job_detail'

jobs_file = File.open('input/leamhall_jobs.txt')
jobs = Array.new
in_job = false

def set_dates(dates)
   date_re     = %r{\((.*) - (.*)\)}
   d           = date_re.match(dates)
   start_date  = long_years(d[1])
   end_date    = long_years(d[2])
   return start_date, end_date
end

def long_years(date)
   dates = date.split(' ')
   if dates[0] == "present"
     return date
   end
   if dates.count == 1
     year = dates[0].to_i
   else
     year  = dates[1].to_i
   end
   if year > 70 and year < 1900
     year += 1900
   elsif year < 70
     year += 2000
   end
   if dates.count == 1
     date = "#{year}"
   else
     date = "#{dates[0]} #{year}"
   end
end

data = Hash.new
jobs_file.each_line { |line|
   line.chomp!.strip!
   next if line.length == 0
   if line.end_with?(')')
     if data.count > 0
       jobs << Job.new(data)
     end
     data          = Hash.new
     header_array  = line.split(',')
     title         = header_array.shift
     dates         = header_array.pop
     start_date, end_date = set_dates(dates)
     data['start_date']  = start_date
     data['end_date']    = end_date
     data['title']       = title
     data['employer']    = header_array.join(' ').strip!
   elsif data['blurb'].nil?
     data['blurb'] = line
   elsif data['tech'].nil?
     data['tech']  = line
   else
     data['extra'] = line
   end
}
if data.count > 0
   jobs << Job.new(data)
end

jobs.each { |j|
     puts "#{j.employer}"
     puts "#{j.title}: #{j.start_date} - #{j.end_date}"
     puts "#{j.blurb}" unless j.blurb.nil?
     puts "#{j.tech}"  unless j.tech.nil?
     puts
}



Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>