1. Need help with this
- Posted by C Bouzy <eucoder at hotmail.com> Dec 18, 2005
- 515 views
- Last edited Dec 19, 2005
Hi All, I have been attempting to write a function that removes any amount of numbers from the beginning of a string of text, but it is not working correctly. Anyone want to take a crack at this? Example: 01-MyText = MyText Example: 01 - MyText = MyText The hyphen after the numbers is important, because if there is no hyphen after the numbers, the numbers should not be removed. Thanks in advance... ----If you continue to do what you have always done, you will get what you have always gotten.----
2. Re: Need help with this
- Posted by cklester <cklester at yahoo.com> Dec 18, 2005
- 517 views
- Last edited Dec 19, 2005
C Bouzy wrote: > > Example: 01-MyText = MyText > Example: 01 - MyText = MyText What results do you expect given the above input? -=ck "Programming in a state of Euphoria." http://www.cklester.com/euphoria/
3. Re: Need help with this
- Posted by Alex Chamberlain <alex.chamberlain at tiscali.co.uk> Dec 18, 2005
- 539 views
- Last edited Dec 19, 2005
This is my example program - the function you want is in it!
constant NUMBERS = "0123456789" constant WHITE_SPACE = " \n\r\t" -- This is the function you want! Some credit Please! function remove_numbers(sequence text) integer tmp_pos, j sequence tmp_ret tmp_pos = find('-', text) if tmp_pos then tmp_ret = {} j = 0 for i = 1 to tmp_pos do if find(text[i], NUMBERS) or find(text[i], WHITE_SPACE) then j = i else if j = tmp_pos-1 then tmp_ret = text[tmp_pos+1..length(text)] exit else tmp_ret = text exit end if end if end for while find(tmp_ret[1], WHITE_SPACE) do tmp_ret = tmp_ret[2..length(tmp_ret)] end while else tmp_ret = text end if return tmp_ret end function sequence txt, txt_ret txt = "01-MyText" txt_ret = remove_numbers(txt) puts(1, txt_ret & '\n') txt = "01 - MyText" txt_ret = remove_numbers(txt) puts(1, txt_ret & '\n') txt = "01MyText" txt_ret = remove_numbers(txt) puts(1, txt_ret & '\n') txt = "01 - This is a very, very Long Song title.mp3" txt_ret = remove_numbers(txt) puts(1, txt_ret & '\n') puts(1, "Press any key...") while getc(0) = -1 do end while
Just reply if you need anymore help - but I am wondering why you struggled to write this! Alex
4. Re: Need help with this
- Posted by C Bouzy <eucoder at hotmail.com> Dec 18, 2005
- 510 views
- Last edited Dec 19, 2005
Alex Chamberlain wrote: > > Just reply if you need anymore help - but I am wondering why you struggled to > write this! > > Alex Thanks Alex... I have been writing code for nearly 24 hours non-stop, so cut me some slack :) But on a serious note, sometimes we struggle of the simplest problems. It is not the first time for me nor will it be the last. ----If you continue to do what you have always done, you will get what you have always gotten.----
5. Re: Need help with this
- Posted by C Bouzy <eucoder at hotmail.com> Dec 18, 2005
- 533 views
- Last edited Dec 19, 2005
cklester wrote: > What results do you expect given the above input? > > -=ck Thanks CK..Alex got it..... ----If you continue to do what you have always done, you will get what you have always gotten.----
6. Re: Need help with this
- Posted by Juergen Luethje <j.lue at gmx.de> Dec 18, 2005
- 539 views
- Last edited Dec 19, 2005
C Bouzy wrote: > I have been attempting to write a function that removes any amount of > numbers from the beginning of a string of text, but it is not working > correctly. Anyone want to take a crack at this? > > Example: 01-MyText = MyText > Example: 01 - MyText = MyText > The hyphen after the numbers is important, because if there is no hyphen > after the numbers, the numbers should not be removed. Maybe this is what you want:
function verify (integer start, sequence source, sequence charlist) -- searches for the first character in 'source', that is not contained -- in 'charlist', beginning at position 'start'; -- returns the position of that character, or 0 if nothing found -- in: start : 1..length(source) -- source : empty sequence ==> function returns 0 -- charlist: empty sequence ==> function returns 0 if start >= 1 and length(charlist) then for i = start to length(source) do if not find(source[i], charlist) then return i end if end for end if return 0 end function constant NUMERIC = "0123456789.", WHITESPACE = "\t\r\n " global function special_func (sequence source) integer p p = verify(1, source, NUMERIC) if p != 0 then p = verify(p, source, WHITESPACE) if p != 0 and source[p] = '-' then p = verify(p+1, source, WHITESPACE) if p != 0 then return source[p..$] end if end if end if return source end function -- * Demo * -- printf(1, "'%s'\n", {special_func("01-MyText")}) printf(1, "'%s'\n", {special_func("01 - MyText")}) printf(1, "'%s'\n", {special_func("01 MyText")}) printf(1, "'%s'\n", {special_func(" MyText")})
Regards, Juergen -- Have you read a good program lately?
7. Re: Need help with this
- Posted by Juergen Luethje <j.lue at gmx.de> Dec 19, 2005
- 540 views
- Last edited Dec 20, 2005
Me wrote: > C Bouzy wrote: > >> I have been attempting to write a function that removes any amount of >> numbers from the beginning of a string of text, but it is not working >> correctly. Anyone want to take a crack at this? >> >> Example: 01-MyText = MyText >> Example: 01 - MyText = MyText >> The hyphen after the numbers is important, because if there is no hyphen >> after the numbers, the numbers should not be removed. > > Maybe this is what you want: > > > function verify (integer start, sequence source, sequence charlist) > -- searches for the first character in 'source', that is not contained > -- in 'charlist', beginning at position 'start'; > -- returns the position of that character, or 0 if nothing found > -- in: start : 1..length(source) > -- source : empty sequence ==> function returns 0 > -- charlist: empty sequence ==> function returns 0 > > if start >= 1 and length(charlist) then > for i = start to length(source) do > if not find(source[i], charlist) then > return i > end if > end for > end if > return 0 > end function > > > constant > NUMERIC = "0123456789.", > WHITESPACE = "\t\r\n " Thanks to a private message that I got, I realized that the following function did not work like I expected it to do. Two lines must be added. In some cases it still returns results that are different from what Alex' program returns. I don't know what Chris exactly needs. > global function special_func (sequence source) > integer p > > p = verify(1, source, NUMERIC) > if p != 0 then > p = verify(p, source, WHITESPACE) > if p != 0 and source[p] = '-' then > p = verify(p+1, source, WHITESPACE) > if p != 0 then > return source[p..$] -- begin new else return "" -- end new > end if > end if > end if > return source > end function > > > -- * Demo * -- > printf(1, "'%s'\n", {special_func("01-MyText")}) > printf(1, "'%s'\n", {special_func("01 - MyText")}) > printf(1, "'%s'\n", {special_func("01 MyText")}) > printf(1, "'%s'\n", {special_func(" MyText")}) Regards, Juergen -- Have you read a good program lately?