From: dblack@... Date: 2007-07-20T15:52:50+09:00 Subject: Re: line.split with some complicating factors... Hi -- On Fri, 20 Jul 2007, Marc Hoeppner wrote: > Hi all, > > I've been thinking about this for a while but cant figure out an > efficient (or any actually) way to do the following: > > a line contains a list of coordinates (specifying a range) separated by > a "," like so: > > 50586..50639,50795..51859,60005..60058,60256..61236 > > Pulling out the separate coordinates via split is of course rather > simple. But, here's the catch... > I not only need the given ranges, but also the stretches in between (I > am pulling the coordinates out of a large text file, so there is no good > way to manipulate the source). So the whole range is in reality > everything between the first and the last number...However, I have to > treat the originally given ranges and the stretches in between > differently. The given ranges are printed in upcase, the parts not > specified in downcase. Now, a way this could look like would be: > > E50586..50639,50640..50795,E50795..51859,.. > > The "E" at the beginning is then used to identify the original ranges > and as a condition for printing in upcase. > > So, my point being...I have absolutely no clue how to do this. The > procedure would have to split the given ranges but also immediately > write the missing ranges in the correct order. > > If anyone has an idea how to approach this, I could really need some > advice here. I'm not sure if this is an exact fit but see if it helps. This is based on the first string (no E characters, etc.): ranges = str.scan(/(\d+)\.\.(\d+)/).map {|a,b| a.to_i..b.to_i } betweens = str.scan(/(\d+),(\d+)/).map {|a,b| (a.to_i+1)..b.to_i-1 } David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)