1. Euphoria ASCII function revisited
Actually, torpedo the last function source. This one is more compact and
offers a wider range:
---------------------------------------------------------------------------
include get.e
function ascii_table(atom character)
sequence valued_result
valued_result = value(sprintf("%d",character))
return valued_result[2]
end function
print(1,ascii_table('A'))
---------------------------------------------------------------------------
David Gay
"A Beginner's Guide To Euphoria"
http://www.geocities.com/SiliconValley/Vista/4346
2. Re: Euphoria ASCII function revisited
> Actually, torpedo the last function source. This one is more compact and
> offers a wider range:
>
> ---------------------------------------------------------------------------
> include get.e
>
> function ascii_table(atom character)
> sequence valued_result
>
> valued_result = value(sprintf("%d",character))
> return valued_result[2]
> end function
>
> print(1,ascii_table('A'))
I think youre complicating your life!
Each charater is represented in Euphoria as it's ASCII value... so
you DON'T need any special function for getting it's value.
Example:
1.- You want to know the ASCII value of letter A:
atom A_ASCII
A_ASCII='A'
2.- You want to print the ASCII value of letter A:
print('A') -- or print(A_ASCII) from above example.
-- or ? 'A'
-- or sprintf("some text %d",'A')
-- or puts(1,'A')
-- etc...
Regards,
Daniel Berstein
danielberstein at usa.net
http://www.geocities.com/SiliconValley/Heights/9316
3. Re: Euphoria ASCII function revisited
Daniel, I was aware that an atom could directly be used to obtain both
the ASCII character and code. My assumption at the time was that the
author wanted to treat it as some
sort of table lookup rather than an absolute value reference. The first
function I placed was originally an ASCII Table, but remapped to offer a
different sort sequence (I took out the remapped table before posting
here).
Hope this clarifies things.
David
4. Re: Euphoria ASCII function revisited
Why I wanted the ASCII function was to do an if-then from a letter input,
such as y for yes and n for no. Here's what I have so far.
---------------------------Cut Here-------------------------------------
include get.e
function ascii_table(object character)
sequence valued_result
valued_result = value(sprintf("%d",character))
character = 0 -- convert valued_result to an atom
character = character + valued_result[2]
return character
end function
atom yn,yn2
yn2 = 0
puts(1,"\nEnter y/n: ") -- user enters y or n in response to a question
yn = wait_key()
function ifyn(atom yn) -- convert y or n to ascii
yn = ascii_table(yn)
return yn
end function
while 1 do -- test for y or n and output a response
if yn = 121 or yn = 89 then -- accept upper or lower case y
puts(1, "\nyes")
exit
elsif
yn = 110 or yn = 78 then -- accept upper or lower case n
puts(1,"\nno")
exit
else -- reject all responses except y or n
while 1 do
puts(1,"\nYou must enter y or n.")
yn = wait_key()
yn = ascii_table(yn)
if yn = 121 or yn = 110 or yn = 89 or yn = 78 then
exit
end if
end while
end if
end while
puts(1,"\nyou entered: ") puts(1,yn) -- output of y or n
5. Re: Euphoria ASCII function revisited
Steve Elder wrote:
>Why I wanted the ASCII function was to do an if-then from a letter input=
,
>such as y for yes and n for no. Here's what I have so far.
Your code was a little "beefed up", I guess you could say. It would be=
easier, instead of using all of the functions you did, to do something li=
ke
this:
--------------------------------------Start of
Code-------------------------------------------------------------
include get.e
object k
while 1 do
puts(1,"\nEnter y/n ")
k =3D wait_key()
if k =3D 'y' or k =3D 'Y' then
puts(1,"\nyes")
exit
elsif k =3D 'n' or k =3D 'N' then
puts(1,"\nno")
exit
else
puts(1,"You must enter y or n")
end while
printf(1,"You entered %s as the answer.",k)
--------------------------------------End of
code----------------------------------------------------------------
It's easier to just put it all in one while loop rather than use up extra=
memory and time with the functions. Also, it's easier to put the letters=
in single quotes than to look up all the ASCII numbers, as 'Y' is equal t=
o
121 (I think). I hope this helps.
Regards,
Bryan Watts
6. Re: Euphoria ASCII function revisited
--OK, now I have it down to:
include get.e
atom k
while 1 do
puts(1,"\nEnter y/n/q to quit: ")
k = wait_key()
if k = 'q' or k = 'Q' then
exit
end if
if k = 'y' or k = 'Y' then
puts(1,"\nyes")
elsif k = 'n' or k = 'N' then
puts(1,"\nno")
else
puts(1,"\nYou must enter y or n")
end if
printf(1,"\nYou entered %s as the answer.",k)
end while
7. Re: Euphoria ASCII function revisited
>Why I wanted the ASCII function was to do an if-then from a letter input,
>such as y for yes and n for no. Here's what I have so far.
(code snipped after analyzing)
Aha! aha! I knew you were looking for some sort of table lookup. :)
But you do not need a function for that. Just do a comparison on the
character values 'y' and 'n' (you can get rid of any checking for the
uppercase 'Y' and 'N' if you use lower() to convert the keyboard input to
lowercase) using the If statement. Euphoria will automatically convert them
to numbers for you, which are the actual ASCII codes for these characters
:)
David Gay
"A Beginner's Guide To Euphoria"
http://www.geocities.com/SiliconValley/Vista/4346
8. Re: Euphoria ASCII function revisited
Ok. Here is my 2 cents worth.
NO need for include get.e
integer key
key = 0
while find(key, "YyNn") = 0 do--loop until Y, y, N, or n is pressed
key = get_key()
end while
if key = 'y' then-- y was pressed
puts(1, "y was pressed\n")
elsif key = 'Y' then-- Y was pressed
puts(1, "y was pressed\n")
elsif key = 'n' then
puts(1, "n was pressed\n")
elsif key = 'N' then-- N was pressed
puts(1, "N was pressed\n")
end if
--Lucius Lamar Hilley III
-- E-mail at luciuslhilleyiii at juno.com
-- I support transferring of files less than 60K.
-- I can Decode both UU and Base64 format.
9. Re: Euphoria ASCII function revisited
Lucius L. Hilley wrote:
>Ok. Here is my 2 cents worth.
>NO need for include get.e
-------code snipped----------
Thanks for opening my eyes to get_key(). I was under the impression=
that get_key() was not a built in function, but called from get.e as is
wait_key() I'm learning as the days go by..
Also, for Steve Elder, I thought it would be nice to, instead of
writing that keypress code over and over every time you want to check for=
input, have a function that will allow you to pass a sequence to it
including the keys you want to look for. I was thinking something like:
check_keypress({y,n,q,b,t})
and it would only return if you pressed any of those letters. It could b=
e
used for menus or choices or whatnot. It would go something like this:
---------code is here----------
include wildcard.e
function check_keypress(sequence look_for)
integer k, m
look_for =3D lower(look_for)
while m =3D 0 do
k =3D lower(get_key())
m =3D match(k, look_for)
end while
return look_for[m]
end function =
I'm sure there are many ways in which this could be improved, but I think=
I
did a pretty good job the first time. I'd appreciate any suggestions.
Regards,
Bryan Watts
10. Re: Euphoria ASCII function revisited
-- You can include wildcard.e, and convert the inputs to lower case,
-- Which will shorten the elsif section. This code will loop until
-- you press q to quit.
include wildcard.e
integer key
while 1 do
key = 0
while find(key, "YyNnQq") = 0 do -- loop until Y, y, N, n, Q, or q is
pressed
key = get_key()
key = lower(key) -- converts to lower case
end while
if key = 'y' then -- y was pressed
puts(1, "y was pressed\n")
elsif key = 'n' then
puts(1, "n was pressed\n")
elsif key = 'q' then
puts(1, "\nThank you for using this program.")
exit
end if
end while
--Steve Elder
11. Re: Euphoria ASCII function revisited
Steve Elder wrote:
>-- You can include wildcard.e, and convert the inputs to lower case,
>-- Which will shorten the elsif section. This code will loop until
>-- you press q to quit.
---code snipped------
I found a few ways to cut down on the amount of code since the last ti=
me
you posted it. First of all, the =
key =3D get_key()
key =3D lower(key) -- converts to lower case
can be cut to
key =3D lower(get_key())
and with this, you only need to put =
while find(key,"ynq") =3D 0 since they will all already be lowercase.
Regards,
Bryan Watts
P.S. What did you think of that function I wrote to let you pass which
letters you want to look for?