1. get() gets() etc...

Opinion:
It seems to me that the single most confusing thing about
Euphoria for beginners is the use of get() and gets(), that
is, how to input stuff correctly.
Part of the confusion is because many people are accustomed
to BASIC. (I'm not. I had to look up INPUT in the basic manual!)
Even so, INPUT is easier to use than Euphoria's input commands.

Fact:
A grep on my GUI database (which undeniably DOES input stuff)
shows that get() is never used, and gets() is used only once;
to read in the database file itself. Never for console input.

Problem:
Beginners with Euphoria aren't necessarily ready or able
to take advantage of all the neat add-on includes (like font.e)
which handle the input transparently. They probably just want to
write a working program, as simply as possible.

Solution:
1. Someone writes a really clear tutorial on using input.
2. Someone writes a small .e file to handle this task more
   smoothly.

Comments?

Irv

new topic     » topic index » view message » categorize

2. Re: get() gets() etc...

On Tue, 9 Jun 1998, Irv wrote:

> Solution:
> 1. Someone writes a really clear tutorial on using input.

Euphoria has several input functions:
get(num)    -- Reads a Euphorian object from a file.
               An object is *anything* that can be represented by a
               Euphorian data structure.
               Found in get.e. Uses gets(num) and value() from get.e
               get() [and value()] return]s[ a two element sequence
               containing whether the get() was successful, and the value
               of the object.
               If num = 0 then it attempts to read from the keyboard.

getc(num)   -- Reads a character from a file.
               If num = 0 then it reads the keyboard, however, if you
               really want to input a character at a time from the
               keyboard, get_key() and wait_key() are better choices.

gets(num)   -- Read a whole string (i.e. everything upto a newline '/n')
               from a file.
               If it's the last line in the file, there might not be a
               newline at the end of it.
               If num = 0 then the string is read from the keyboard, and a
               string inputted in this way *always* has a newline at the
               end of it.

get_key()   -- Reads a key on the keyboard and returns a value.
               If it's a standard ASCII key pressed, the correct code is
               returned e.g. 'a' = 97, 'A' (Shift-a) = 65 etc.
               If it's an "advanced" key like F5, Cursor Right or
               Ctrl-PgUp, a special code ( >= 256 ) is returned.
               If there's no key pressed get_key() returns -1.

wait_key()  -- Works just like get_key() except it waits for a key to be
               pressed.
               wait_key() could be emulated by a loop with get_key(), but
               if you need it, use it. Emulating with get_key() slows the
               Operating System down.

> 2. Someone writes a small .e file to handle this task more
>    smoothly.

Many folks around here that have come from the BASIC world, have written a
simple "input()" function. It doesn't quite operate like the BASIC version
but it comes close:

include get.e

function input(sequence prompt)
    -- get2.e contains a larger version or input()
    sequence out

    puts(1, prompt)

    out = gets(0)
    out = out[1..length(out)-1] -- strip newline

    return out
end function

function inputnum(sequence prompt)
    sequence data, temporary
    atom out

    out = 0.0
    data = input(prompt) -- Sneaky :)

    for i = 1 to length(data) do -- This loop is optional...
        if find(data[i], ".0123456789-+eE") then
            temporary = temporary & data[i]
        end if
    end for

    data = value(temporary)

    if data[1] = GET_SUCCESS then
        out = data[2]
    end if

    return out
end function

-- How to use:
sequence name
atom age

name = input("What is your name? > ")
age = inputnum("How old are you? > ")

-- -- The (Q)Basic would be:
-- DIM name AS STRING
-- DIM age AS DOUBLE
--
-- INPUT "What is your name? > ", name
-- INPUT "How old are you? > ", name

--
Carl R White
E-mail...: cyrek- at -bigfoot.com              / Remove the hyphens before
Finger...: crwhite- at -dcsun1.comp.brad.ac.uk \ mailing or fingering...
Url......: http://www.bigfoot.com/~cyrek/

new topic     » goto parent     » topic index » view message » categorize

3. Re: get() gets() etc...

At 04:08 PM 6/9/98 +0100, Carl R. White wrote:
>
>On Tue, 9 Jun 1998, Irv wrote:
>
>> Solution:
>> 1. Someone writes a really clear tutorial on using input.
>
Thanks. I'll create a "beginners" page for questions like this.
Any comments/additions to Carl's tutorial are welcome.
(e-mail to mountains at mindspring.com, please)

Carl's Input Info:

>Euphoria has several input functions:
>get(num)    -- Reads a Euphorian object from a file.
>               An object is *anything* that can be represented by a
>               Euphorian data structure.
>               Found in get.e. Uses gets(num) and value() from get.e
>               get() [and value()] return]s[ a two element sequence
>               containing whether the get() was successful, and the value
>               of the object.
>               If num = 0 then it attempts to read from the keyboard.
>
>getc(num)   -- Reads a character from a file.
>               If num = 0 then it reads the keyboard, however, if you
>               really want to input a character at a time from the
>               keyboard, get_key() and wait_key() are better choices.
>
>gets(num)   -- Read a whole string (i.e. everything upto a newline '/n')
>               from a file.
>               If it's the last line in the file, there might not be a
>               newline at the end of it.
>               If num = 0 then the string is read from the keyboard, and a
>               string inputted in this way *always* has a newline at the
>               end of it.
>
>get_key()   -- Reads a key on the keyboard and returns a value.
>               If it's a standard ASCII key pressed, the correct code is
>               returned e.g. 'a' = 97, 'A' (Shift-a) = 65 etc.
>               If it's an "advanced" key like F5, Cursor Right or
>               Ctrl-PgUp, a special code ( >= 256 ) is returned.
>               If there's no key pressed get_key() returns -1.
>
>wait_key()  -- Works just like get_key() except it waits for a key to be
>               pressed.
>               wait_key() could be emulated by a loop with get_key(), but
>               if you need it, use it. Emulating with get_key() slows the
>               Operating System down.
>
>> 2. Someone writes a small .e file to handle this task more
>>    smoothly.
>
>Many folks around here that have come from the BASIC world, have written a
>simple "input()" function. It doesn't quite operate like the BASIC version
>but it comes close:
>
>include get.e
>
>function input(sequence prompt)
>    -- get2.e contains a larger version or input()
>    sequence out
>
>    puts(1, prompt)
>
>    out = gets(0)
>    out = out[1..length(out)-1] -- strip newline
>
>    return out
>end function
>
>function inputnum(sequence prompt)
>    sequence data, temporary
>    atom out
>
>    out = 0.0
>    data = input(prompt) -- Sneaky :)
>
>    for i = 1 to length(data) do -- This loop is optional...
>        if find(data[i], ".0123456789-+eE") then
>            temporary = temporary & data[i]
>        end if
>    end for
>
>    data = value(temporary)
>
>    if data[1] = GET_SUCCESS then
>        out = data[2]
>    end if
>
>    return out
>end function
>
>-- How to use:
>sequence name
>atom age
>
>name = input("What is your name? > ")
>age = inputnum("How old are you? > ")
>
>-- -- The (Q)Basic would be:
>-- DIM name AS STRING
>-- DIM age AS DOUBLE
>--
>-- INPUT "What is your name? > ", name
>-- INPUT "How old are you? > ", name
>
>--
>Carl R White

new topic     » goto parent     » topic index » view message » categorize

4. Re: get() gets() etc...

>Problem:
>Beginners with Euphoria aren't necessarily ready or able
>to take advantage of all the neat add-on includes (like font.e)
>which handle the input transparently. They probably just want to
>write a working program, as simply as possible.
>
>Solution:
>1. Someone writes a really clear tutorial on using input.
>2. Someone writes a small .e file to handle this task more
>   smoothly.


A file is cached anyway. Euphoria works with sequences to main data. Dos
works with files to maintain data.
Why not have a sequence returned from the file?
Off course, not the entire file is loaded. But the parts to be loaded could
be optimized at those points it already could and people will not need to
learn new tricks to manipulate data maintained as DOS does.
The file function has a file name argument and one other argument called
interpretation-method.
This is either TEXT (the \r removed and split into sequences of lines), DATA
(one long sequence containing all the file's data), OBJECT (a readable
representation of an euphoria object like: " { 1, 2, 3 }") or IMAGE (an
espace fficient, maybe even compressed representation of an euphoria object)

Files interpreted as OBJECT, return a sequence where every element is the
next readable representation of an object.

All these methods work with caching, and do not load the entire file to
memory, althrough it does appear this way for the programmer. Now Euphoria
is able to see what you want to do with your data, it can pre-compile the
steps of cashing right-into your program, thereby optimizing. It could, for
example, optimize loops extremely.
Also, when you need the entire data anyway, it could predict this, and be
*very* fast, compared to the gets () .. eof check.. append.. loop that you
need to use now, which is one of the things that makes my edo_load so much
slower than it *could* be.

Also, there needs to be a routine to fluss our cache. I mean, force Euphoria
to update the file with the cache. There are situation where direct
communication with a file in needed.

We could also call this thing, just before a place, where we will be needed
a lot of memory, but then again, I assume Euphoria keeps the memory
allocated constantly.

Think about it. It will make a difference for beginners. For performace. And
it makes Euphoria syntax a lot cleaner.
One simple assignment of a text-image to a file-sequence of "LPT1" will
print out the text-image!

Surely, we will still have streams, but files were never streams anyway. I
really do not know who made that up.
Because, how will the input and output of devices be done. They *are*
streams, unlike files? (or at least, they *have* to be streams, but they
could also be a file.). However the input and output can now have
specialized routines that dont' need to work with files. That's not what
they're made for anyway. Puts () and gets () are now a compromize between
file input and keyboard input. The we can have *real* streaming inputs.

Also, I vote for a routine that allows you to change the value atoms are
cut. Now they are cut per 8 bits. But why not more or less ? We now have
special routines to poke an integer as 4 bytes. Why not use the normal poke
and call a routine to set the cut-off at 32 bits!

Also, the file-sequence scheme could be adapted for allocated memory also.
We'll improve the speed and clearity too.

Pure Euphorianic sounds nice, but the way we need to acces the system, its
i/O, Ports (this has to be a built-in thing!), Memory, DLL's, interrupts is
conversion only. And with this conversion all safety, clearity and the
powerfull syntaxes are trown away. Or at least, that's IMHO.

Ralf Nieuwenhuijsen
nieuwen at xs4all.nl

new topic     » goto parent     » topic index » view message » categorize

5. Re: get() gets() etc...

Ralf Nieuwenhuijsen wrote:
>
> A file is cached anyway. Euphoria works with sequences to main data. Dos
> works with files to maintain data.
> Why not have a sequence returned from the file?
> Off course, not the entire file is loaded. But the parts to be loaded could
> be optimized at those points it already could and people will not need to
> learn new tricks to manipulate data maintained as DOS does.
> The file function has a file name argument and one other argument called
> interpretation-method.
> This is either TEXT (the \r removed and split into sequences of lines)

I support an idea like Ralf's. For me, text files are the main issue. I
certainly support a few added languge elements that will simplify
working with text files and making the operations lightning fast. Ralf
has several good ideas. I am merely putting my emphasis on improved,
simplified, and faster text file handling. Single dimension sequences
for text files as Ralf suggests just makes good sense to me.

--
Terry Constant
constant at flash.net

new topic     » goto parent     » topic index » view message » categorize

6. Re: get() gets() etc...

>1. Someone writes a really clear tutorial on using input.

This would be helpful.

>2. Someone writes a small .e file to handle this task more
>   smoothly.

David Cuny has one on the Recent Contributions page. With commas.e (mine,
yeah, a shameless plug... :), you can do something like this:

number = remove_commas(gets(0))

To get a number. If you used get(), you'd need to do this:

junk = get(0)
if junk[1] = GET_SUCCESS then
    number = junk[2]
else
    number = 0
end if

Also, if the user just pressed ENTER, without typing anything, get()
would go to the next line and keep waiting until you type something other
than whitespace. My method assumes 0. (Like BASIC's INPUT) It also
allows:

$3,000
or
50%

Then again, it also allows:

4,4,4
(444)

and

44$45%%%443
(4445443)

Oh well....

(PS, if you coupled David Cuny's with mine, you'd have a SLE like in
TextGUI that can have default values like $5.00 or $5,000.00 [That's what
I did in one program that used TextGUI (using the TextGUI input stuff
[SLE]), and I think the effect is neat..])

Any better ideas? (Like a function to handle all of this, maybe something
that accepts a % code (%d, %f, etc) to determine the return value. (%c
would be single character [ie wait_key()])

_____________________________________________________________________
You don't need to buy Internet access to use free Internet e-mail.
Get completely free e-mail from Juno at http://www.juno.com
Or call Juno at (800) 654-JUNO [654-5866]

new topic     » goto parent     » topic index » view message » categorize

7. Re: get() gets() etc...

At 04:08 PM 6/9/98 +0100, Carl R. White wrote:
A simple input routine.

Note: it's missing a line in routine inputnum()
-- see the correction below, and add it to your copy.

Irv

>include get.e
>
>function input(sequence prompt)
>    -- get2.e contains a larger version or input()
>    sequence out
>
>    puts(1, prompt)
>
>    out = gets(0)
>    out = out[1..length(out)-1] -- strip newline
>
>    return out
>end function
>
>function inputnum(sequence prompt)
>    sequence data, temporary
>    atom out
>
>    out = 0.0
     temporary = {} --*** add this line, or the routine will fail!

>    data = input(prompt) -- Sneaky :)
>
>    for i = 1 to length(data) do -- This loop is optional...
>        if find(data[i], ".0123456789-+eE") then
>            temporary = temporary & data[i]
>        end if
>    end for
>
>    data = value(temporary)
>
>    if data[1] = GET_SUCCESS then
>        out = data[2]
>    end if
>
>    return out
>end function
>
>-- How to use:
>sequence name
>atom age
>
>name = input("What is your name? > ")
>age = inputnum("How old are you? > ")
>

new topic     » goto parent     » topic index » view message » categorize

8. Re: get() gets() etc...

On Tue, 9 Jun 1998, Irv wrote:

> At 04:08 PM 6/9/98 +0100, Carl R. White wrote:
> A simple input routine.
>
> Note: it's missing a line in routine inputnum()
> -- see the correction below, and add it to your copy.
>
> Irv
[snip excess]
> >    out = 0.0
>      temporary = {} --*** add this line, or the routine will fail!

Oops! That's what I get for coding on the fly on a machine that doesn't
run Euphoria... :)

--
Carl R White
E-mail...: cyrek- at -bigfoot.com              / Remove the hyphens before
Finger...: crwhite- at -dcsun1.comp.brad.ac.uk \ mailing or fingering...
Url......: http://www.bigfoot.com/~cyrek/

new topic     » goto parent     » topic index » view message » categorize

9. Re: get() gets() etc...

>Also, when you need the entire data anyway, it could predict this, and be
>*very* fast, compared to the gets () .. eof check.. append.. loop that you
>need to use now, which is one of the things that makes my edo_load so much
>slower than it *could* be.

In BASIC there is a LOF() function which returns the length of the file and a
BLOAD which lets you quickly load a whole file or a large part into memory
(starting at the beginning, unfortunately).  I have no idea if Euphoria
supports this, but it would seem pretty necessary if we want any kind of
speed.

new topic     » goto parent     » topic index » view message » categorize

10. Re: get() gets() etc...

>In BASIC there is a LOF() function which returns the length of the file and a

(assumes file fn opened as binary)
integer junk,LOF
junk=seek(fn,-1)
LOF=where(fn)

>BLOAD which lets you quickly load a whole file or a large part into memory
>(starting at the beginning, unfortunately).  I have no idea if Euphoria
>supports this, but it would seem pretty necessary if we want any kind of
>speed.

function bload(sequence path)
    integer fn,c
    sequence r
    r={}
    fn=open(path,"rb")
    c=getc(fn)
    while c!=-1 do
        r=r&c
        c=getc(fn)
    end while
    return r
end function

----------------------------------------------------

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu