Re: Final iteration lost?
Hassan Schroeder <[email protected]> Thu, 15 Aug 2019 07:20:09 -0700
| Newsgroups | gmane.comp.lang.ruby.general |
|---|---|
| Message-ID | <CACmC4yBA+cVqvhJ3N7LL2z0JS4QSXpaGqGFr5px3-pwbefVDmg@mail.gmail.com> |
On Thu, Aug 15, 2019 at 4:31 AM Leam Hall <[email protected]> wrote: > header_array = line.split(',') > title = header_array.shift > dates = header_array.pop > start_date, end_date = set_dates(dates) > employer = header_array.join(' ').strip! > data = { start_date: start_date, end_date: end_date, > title: title, employer: employer } Not everyone loves regexes :-) but a possible alternative to the above: irb(main):008:0> line = "Linux Admin, Spidernet s.r.l, Repubblica Italiana, (1997 - 1998)" => "Linux Admin, Spidernet s.r.l, Repubblica Italiana, (1997 - 1998)" irb(main):009:0> /\A(?<title>[\w ]+),\s*(?<employer>[^,]+).+\((?<start_date>.+) - (?<end_date>.+)\)\z/.match(line).named_captures => {"title"=>"Linux Admin", "employer"=>"Spidernet s.r.l", "start_date"=>"1997", "end_date"=>"1998"} irb(main):010:0> FWIW! -- Hassan Schroeder ------------------------ [email protected] twitter: @hassan Consulting Availability : Silicon Valley or remote Unsubscribe: <mailto:[email protected]?subject=unsubscribe> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>