[Tutor] ratios with regular expressions, split?
Remco Gerlich
scarblac@pino.selwerd.nl
Thu, 12 Jul 2001 16:11:00 +0200
On 0, kevin parks <kp87@lycos.com> wrote:
> Additionally the ratios might also be input as 7:6 or 7/6 and some have a
> different number of digits, like 3/2, 81/80 and 127/128, etc. Also there is
> no way to tell in advance if there will be a series of 3 ratios, 4 ratios,
> or 53 ratios. What is the best way to do this? with regex or split? I bought
> the Chun book and it has a regular expressions chapter that i am looking at
> now, but i am not sure exactly the best way to get started on this.
Could you hit enter now and then? I saw this whole paragraph on one line,
and that's not that easy to read. (reads below). Oops. Happy Birthday! :-)
Anyway, I think that split would work fine. As long as you're certain the
ratios always have a simple n/m format and are seperated by commas,
something like
l = "3/4, 4/5, 6/7"
ratios = []
for ratio in l.split(","):
n, m = ratio.split("/")
ratios.append(Ratio(n, m))
should work fine.
--
Remco Gerlich