Re: Getting atom values with gets()
- Posted by Patrick Quist <quistnet at HOTMAIL.COM> Jun 10, 1999
- 405 views
>From: Derek Brown <Cyrusbrown at AOL.COM> >Reply-To: Euphoria Programming for MS-DOS <EUPHORIA at LISTSERV.MUOHIO.EDU> >To: EUPHORIA at LISTSERV.MUOHIO.EDU >Subject: Getting atom values with gets() >Date: Wed, 9 Jun 1999 21:23:22 EDT > > Sorry for the slew of questions lately.. I'm working on a big project. >Details will be out in a week or so once more work on it is complete. > My question is about the gets() or get() commands. I need to be able to >input data as an atom...ie if I type 100 gets() will return 100, not >{XX,YY,YY,10}. Or I need to take the latter and convert it to 100. This >is >probably as easy as tying my shoes, but I'm stumped. I'll probably kick >myself when I see how easy it is ; ) > >Thanks, >Derek Brown You could also use gets() instead of get(), you can return a sequence if it's a sequence, and an atom if the sequence is an atom. Use the source beneath... E.g. --------- include seq2int.e object input input=gets(0) -- Get input if isAtom(input) then -- If the sequence is really an atom input=seq2int(input) -- Sequence becomes an atom... end if --------- [SEQ2INT.E] global function isAtom(sequence input) object returnment,tmp tmp=0 for a=1 to length(input) do if input[a]>='0' and input[a]<='9' then tmp+=1 elsif input[a]='.' or input[a]='-' then tmp+=1 end if end for if tmp=length(input) then returnment=0 else returnment=-1 end if return returnment end function -- Works till "9999999999", try "1.5" or "-1.5" it works -- global function seq2int(sequence str) object result,tmp1,tmp2,divide divide=1 tmp1=find('.',str) tmp2=find('-',str) if tmp1 !=0 then divide=power(10,length(str)-tmp1) str=str[1..tmp1-1]&str[tmp1+1..length(str)] end if if tmp2 !=0 then divide*=-1 str=str[1..tmp2-1]&str[tmp2+1..length(str)] end if result=str[length(str)]-48 for a=1 to length(str)-1 do result+=power(10,length(str)-a)*(str[a]-48) end for result/=divide return result end function [EOF] Bye, PQ QC ______________________________________________________ Get Your Private, Free Email at http://www.hotmail.com