From: Lloyd Linklater Date: 2008-09-19T03:15:30+09:00 Subject: Re: Suggestion for string parsing Me Me wrote: > >> What is the *actual* problem? If it is what you said ("I would like to >> know if there's a better way to parse a string and assing values to >> variables;") then RegEx is a fine solution. If you reject a good >> solution and seek something else, then it can only be that you are >> actually seeking a solution to a different problem. So, what are you >> *really* looking for? > > I'm quite new to Ruby and I can understand that athere are better way to > do things, what I would like to avoid is to write something like this > (that works) > > line =~ /(\w*)=([0-9A-Za-z -.:]*),([0-9A-Za-z -.:]*),([0-9A-Za-z > -.:]*),([0-9]*),([0-9]*),([0-9]*),([0-9]*),([0-9]*),([0-9]*),([0-9]*),([0-9]*),([0-9]*),([0-9]*)/ > > I wanted to this in one line but in a more mainable way, otherwise I > could always pars the string char by char. AHA! I understand, or I at least flatter myself that I do. how about this: require 'scanf' s = "Client=MPEG-4,390000,700000,24000,9452349,234583475,2452345" val = s.scanf("%6s=%s") vals = val[1].split(",") p vals => ["MPEG-4", "390000", "700000", "24000", "9452349", "234583475", "2452345"] -- Posted via http://www.ruby-forum.com/.