1. confused...
- Posted by Burt Feb 03, 2009
- 1007 views
Hi,
I'm just getting started with Euphoria and programming, and there's something in the documentation that's confusing me. It's regarding the "get()" function. It says in the manual that:
"get() can read arbitrarily complicated Euphoria objects. You could have a long sequence of values in braces and separated by commas, e.g. {23, {49, 57}, 0.5, -1, 99, 'A', "john"}. A single call to get() will read in this entire sequence and return it's value as a result."
But when I tried inputting this example sequence, I got an error message: "sequence found inside character string". But I thought a character string IS a sequence, and sequences can be nested, can't they?
Also, for the same function, the manual follows with this:
"Each call to get() picks up where the previous call left off. For instance, a series of 5 calls to get() would be needed to read in: 99 5.2 {1,2,3} "Hello" -1"
On the face of it, this seems to conflict with the previous paragraph which says that "a *single* call to get() will read in...."
I hope someone can clear this up for me. cheers,
2. Re: confused...
- Posted by achury Feb 03, 2009
- 936 views
The variable that receives this value must be declared as sequence or object, never as string. Please check this and post all the code.
3. Re: confused...
- Posted by gbonvehi Feb 03, 2009
- 941 views
Hi Burt, Welcome to Euphoria, I hope you like it.
Regarding the error, did you, by any chance, tried to print it? Procedures likes put() can't handle a sequence inside a sequence.
Regards,
4. Re: confused...
- Posted by mattlewis (admin) Feb 03, 2009
- 950 views
But when I tried inputting this example sequence, I got an error message: "sequence found inside character string". But I thought a character string IS a sequence, and sequences can be nested, can't they?
Could you post the code that you used? It sounds like you're trying to print the result, which is where I believe this error message would come from. You might try printing out using the question mark, or print instead of puts or printf.
Also, for the same function, the manual follows with this:
"Each call to get() picks up where the previous call left off. For instance, a series of 5 calls to get() would be needed to read in: 99 5.2 {1,2,3} "Hello" -1"
On the face of it, this seems to conflict with the previous paragraph which says that "a *single* call to get() will read in...."
Note that there are actually five different objects in the example:
- 99: an integer
- 5.2: an atom
- {1,2,3}: a sequence
- "Hello": a string of characters (which is actually identical to the sequence {72,101,108,108,111}
- -1: an integer
Matt
5. Re: confused...
- Posted by Burt Feb 03, 2009
- 946 views
The variable that receives this value must be declared as sequence or object, never as string. Please check this and post all the code.
But isn't a string the same as a sequence? I did declare the variable as a sequence.
6. Re: confused...
- Posted by Burt Feb 03, 2009
- 948 views
Hi Burt, Welcome to Euphoria, I hope you like it.
Regarding the error, did you, by any chance, tried to print it? Procedures likes put() can't handle a sequence inside a sequence.
Regards,
Hi Guillermo,
Yes, I think that was the problem, when I change the code (as Matt suggested) to:
include get.e sequence input puts(1, "enter some text ") input = get(0) if input[1] != 1 then ? input[2] else puts(1, "not a valid euphoria object!") end if
There was no error message, but the output wasn't what I was expecting. In the manual it says:
Actually, I'm not entirely clear what a "valid" euphoria object is in this context! In the manual it says:
"Input, from file fn, a human-readable string of characters representing a Euphoria object."
Nor am I sure what "human readable" means...
So if I wanted to read in from the keyboard a list of numbers and or strings, would I need to use a loop? and if I wanted to print out what I'd input would I need to test the elements of the sequence to determine which was a string and which was a number. I have a feeling I'm making this more complicated than it actually is.
7. Re: confused...
- Posted by achury Feb 03, 2009
- 939 views
The variable that receives this value must be declared as sequence or object, never as string. Please check this and post all the code.
But isn't a string the same as a sequence? I did declare the variable as a sequence.
Is not the same. A string is a special case of sequence wich all the elements are integers, so fit well to represent text strings. So all the string ares sequences but not all the sequences are strings
8. Re: confused...
- Posted by Burt Feb 03, 2009
- 935 views
For example, suppose I wanted to create a little database, so I type the following on one line:
nuts 1.20 bolts 3.30 washers 2.50 screws 1.50
Here is the code I wrote to do it:
include get.e sequence input puts(1, "enter the items\n") while 1 do input = get(0) if input[1] = 1 then exit end if end while
But it's not working, nothing is going into input. Where have I gone wrong?
9. Re: confused...
- Posted by Burt Feb 03, 2009
- 939 views
The variable that receives this value must be declared as sequence or object, never as string. Please check this and post all the code.
But isn't a string the same as a sequence? I did declare the variable as a sequence.
Is not the same. A string is a special case of sequence wich all the elements are integers, so fit well to represent text strings. So all the string ares sequences but not all the sequences are strings
But I thought you could only declare variables as one of the following:
- object
- sequence
- atom
- integer
How do I declare a variable as a string?
10. Re: confused...
- Posted by bryanso Feb 03, 2009
- 935 views
Dear Burt,
If you enter this as input:
nuts 1.20 bolts 3.30 washers 2.50 screws 1.50
Then it won't work because the above is not a Euphoria object. A euphoria object is like this:
{ 1 {23 2} "abc"}
You may try
"nuts" 1.20 "bolts" 3.30 "washers" ...
Then it will probably work. The first get() will get "nuts". The second get() will get 1.20 etc.
Bryan
11. Re: confused...
- Posted by mattlewis (admin) Feb 03, 2009
- 1026 views
In the manual it says:
Actually, I'm not entirely clear what a "valid" euphoria object is in this context! In the manual it says:
"Input, from file fn, a human-readable string of characters representing a Euphoria object."
Nor am I sure what "human readable" means...
Human readable, like you'd see in source code, as opposed to some binary (computer readable) format. A valid euphoria object, such that you could use it in source code, so something like:
{1,2,,3
...would obviously not be well formed, and therefore not a valid euphoria object.
So if I wanted to read in from the keyboard a list of numbers and or strings, would I need to use a loop? and if I wanted to print out what I'd input would I need to test the elements of the sequence to determine which was a string and which was a number.
If you're taking keyboard input from the console, then you'll probably want to put them into a loop. Take a look at prompt_string() in get.e (or std/console.e if you're using euphoria 4.0). It will return a sequence with the characters returned. Then you'll need to use the value() function (also in get.e) to convert the raw input into an object like you want.
If you just want a string, then prompt_string() will be fine. There's also prompt_number(), which returns an atom.
Matt
12. Re: confused...
- Posted by DerekParnell (admin) Feb 03, 2009
- 983 views
I'm just getting started with Euphoria and programming,
Welcome to both programming and Euphoria. Glad to have you, and this is the place to ask questions.
and there's something in the documentation that's confusing me. It's regarding the "get()" function. It says in the manual that:
"get() can read arbitrarily complicated Euphoria objects. You could have a long sequence of values in braces and separated by commas, e.g. {23, {49, 57}, 0.5, -1, 99, 'A', "john"}. A single call to get() will read in this entire sequence and return it's value as a result."
But when I tried inputting this example sequence, I got an error message: "sequence found inside character string". But I thought a character string IS a sequence, and sequences can be nested, can't they?
Ok, there are a few things here.
- Yes, each call to get() will read one and only one object at a time. That object can be a number (integer or atom) or a sequence (which includes strings).
- Yes, strings are sequences.
- Yes, sequences can be nested.
However, the message you got "sequence found inside character string" did not come from the get() call, but from a puts() or printf() call. Those routines can only deal with strings.
A string is a certain type of sequence. It is a sequence that consists only of integers in the value range of 0 - 255, and thus must not have nested sequences.
In the example from the manual, it reads in
{23, {49, 57}, 0.5, -1, 99, 'A', "john"}
which is a single sequence containing seven elements. To display this sequence, you cannot use puts() or printf() because it is not a string sequence - it contains subsequences and floating point numbers.
If you need to display this as is, it might be best to use the pretty_print() routine instead ...
pretty_print(1, data, {2})
It all depends on how you want to see the data.
Also, for the same function, the manual follows with this:
"Each call to get() picks up where the previous call left off. For instance, a series of 5 calls to get() would be needed to read in: 99 5.2 {1,2,3} "Hello" -1"
On the face of it, this seems to conflict with the previous paragraph which says that "a *single* call to get() will read in...."
This example shows you five objects whereas the other example showed you one object - a sequence.
Actually, I'm not entirely clear what a "valid" euphoria object is in this context! In the manual it says:
"Input, from file fn, a human-readable string of characters representing a Euphoria object."
Nor am I sure what "human readable" means...
It just means that when you display the file as-is on screen, you can read it - that it contains just ASCII characters - plain text.
This is opposed to machine-readable data which contains bytes that are not meant to be used as if they where ASCII characters, for example a program (executable) file.
So if I wanted to read in from the keyboard a list of numbers and or strings, would I need to use a loop?
Maybe ... it all depends on what you want to do and how you want to do it. One thing about programming is that you get to decide on how you want to do stuff.
If you want to get data from a keyboard, it means that you want a person to type it in - naturally, but this also implies that your program must allow for human-error. In other words, you can not, and must not, expect a person to always type the correct data in.
This is what makes programming a challenge.
I haven't got time just now to finish this discussion, but I'd use the gets() function to read in a whole line of text and then parse it to extract the data elements.
13. Re: confused...
- Posted by SunPsych08 Feb 03, 2009
- 911 views
- Last edited Feb 04, 2009
Hi,
I'm just getting started with Euphoria and programming, and there's something in the documentation that's confusing me. It's regarding the "get()" function.
Hi Burt
I'm not an expert at this so I'm happy to be corrected, but if I write this code:
include get.e sequence got puts(1,"Enter an Eu sequence: ") -- prompt user to enter a sequence got = get(0) -- read the sequence puts(1, '\n') -- new line ? got[2] -- display the entered sequence (in ASCII values) -- haven't checked for user's accuracy - not good practice! if getc(0) then end if -- don't close the window till I've looked at it
and if I enter {1, {2.3, 4}, 'A', "B" } at the prompt, my output is:
{
1,
{2.3, 4},
65,
{66}
}
which is consistent with the doc's: one call to get() has read in the entire sequence.
Alex Caracatsanis
14. Re: confused...
- Posted by achury Feb 04, 2009
- 958 views
The variable that receives this value must be declared as sequence or object, never as string. Please check this and post all the code.
But isn't a string the same as a sequence? I did declare the variable as a sequence.
Is not the same. A string is a special case of sequence wich all the elements are integers, so fit well to represent text strings. So all the string ares sequences but not all the sequences are strings
But I thought you could only declare variables as one of the following:
- object
- sequence
- atom
- integer
How do I declare a variable as a string?
Sorry I'm confusing you more. Those are predefined data types, but you can define your own types.
15. Re: confused...
- Posted by Burt Feb 04, 2009
- 924 views
Wow, thanks guys, this is all really helpful, I think I get it now.
I was wondering whether I'd made the right choice of language in Euphoria, as there doesn't seem to be as much documentation as better known languages (no "Euphoria for Dummies"), but I'm going to stick with it, now that I know that I'm not simply going to be told to RTFM!
I really like the debugging facilities, it makes it so much easier to understand what's happening.
16. Re: confused...
- Posted by Burt Feb 04, 2009
- 1002 views
Something in the manual that caught my eye is:
"Brace brackets are used to construct sequences out of a list of expressions. These expressions can be constant or evaluated at run-time. e.g."
{x+6, 9, y*w+2, sin(0.5)}
So does this mean that a sequence can contain calls to arbitrary user-defined functions? I replaced sin(0.5) with a user-defined function "square".
sequence s integer x, y, w x = 1 y = 2 w = 3 function square(atom x) return x*x end function s = {x + 6, 9, y*w + 2, square(0.5)} ? s[4] -- gives 0.25
This is very cool. I think I'm hooked!