1. Range checking
Hi all,
I'm stuck on a silly problem I can't solve.
I'm quite profane
on things like parsing, scanning and so on, so I need
some help.
I want to write a routine to let the user input some
ranges just like when in Micro$oft Word you want to print a range of pages.
Eg. 1-2,5-10,13,16-20.
Any1 has some ideas ? I tried some but none succeeded.
Many TIA,
Maurizio
---------------------------------------------------------
Maurizio Moroni
Omnitel Pronto Italia SpA
Network Testing Dept.
e-mail : maurizio.moroni at omnitel.it
---------------------------------------------------------
2. Re: Range checking
>Hi all,
>
>I'm stuck on a silly problem I can't solve.
>I'm quite profane
on things like parsing, scanning and so on, so I need
>some help.
>I want to write a routine to let the user input some
>ranges just like when in Micro$oft Word you want to print a range of
pages.
>Eg. 1-2,5-10,13,16-20.
Here is my quick attempt at it. There sure are better, shorter and faster
ways to do this, but this should work. You might need to trim it a little so
that it fits into your program. Call the function parsepages(), you will be
asked to input the ranges, and then a sequence containing all pages to be
printed (or whatever) is returned, eg. {1,2,5,7,8,9}. It will return -1 if
it finds some errors. Input filter should be added, so the user cannot input
stuff like "{"abc",23}", since value will return GET_SUCCESS on this.
Anyway, here it is: (Regards, Einar Mogen)
-----------------------------------------------------------------
include get.e
function parsepages()
sequence data, number, junk, pages
integer ascii, FROM, TO, range, addpage
clear_screen()
puts(1,"Enter ranges:\n")
data = gets(0)
data = data[1..length(data)-1]
range = 0
addpage = 0
number = {}
pages = {}
for counter = 1 to length(data) do
ascii = data[counter]
if ascii = '-' then
range = 1
junk = value(number)
if junk[1] != GET_SUCCESS then
return -1
end if
FROM = junk[2]
number = {}
elsif ascii = ',' then
junk = value(number)
if junk[1] != GET_SUCCESS then
return -1
end if
number = {}
addpage = 1
else
number = number&ascii
end if
if counter = length(data) then
junk = value(number)
if junk[1] != GET_SUCCESS then
return -1
end if
addpage = 1
end if
if addpage then
if range then
TO = junk[2]
if TO < FROM then
return -1
end if
for add = FROM to TO do
pages = append(pages,add)
end for
else
pages = append(pages, junk[2])
end if
range = 0
addpage = 0
end if
end for
return pages
end function