1. Couple of questions about sequences

I am a newcomer still going through the Euphoria documentation, learning the basics. I have a couple of questions about sequences:

1) since strings are stored within sequences, and their internal representation consists merely of integers stored within sequences, how can we tell if the content of a sequence is actually a string or merely a series of integers? I understand that from the language's point of view the two are exactly the same thing, but from the programmer's point of view they are not. Is there a way to store a metadata somewhere to indicate that "this is a string" and is meant to be interpreted and used as such?

2) with regard to the use of named constants to reference the indexes of a sequence, I am assuming that there must be some conventional way of doing it, as this appears to be a basic need for handling sequences. From what I understand, the use of named constants for referencing indexes would be indispensable in the case of sequences whose structure is very dynamic - without constantly keeping track of the ever-changing indexes, the contents of a sequence would soon become inaccessible and the whole sequence would become useless. My question is: what's the best way to store, access and update named constants? I guess the end-result would resemble something like a lookup table. But how to implement it? (BTW, I am not asking about maps/associative arrays).

new topic     » topic index » view message » categorize

2. Re: Couple of questions about sequences

For item 1, I would say that it's the programmer's responsibility to ensure that he passes a string sequence where a string sequence is expected.

If you really want to record type metadata, you could create a custom type where the first element is a type indicator (constant or enum) and a utility function to iterate over a sequence to verify whether or not it is a string.

You would then have to wrap your function calls to send just the string portion after verifying that it is the correct type.

In my experience, the main error in a sequence not being a string isn't that it contains atom or integer data instead of characters, but rather that it consists of subsequences instead of characters. The interpreter will often catch that as a runtime error.

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

3. Re: Couple of questions about sequences

Nevla said...

I am a newcomer still going through the Euphoria documentation, learning the basics. I have a couple of questions about sequences:

I have just chopped up (literally) the Euphoria documentation and I am trying to put the pieces back together again. I'll try to make a PDF so you can try 4.1 out.

Nevla said...

1) since strings are stored within sequences, and their internal representation consists merely of integers stored within sequences, how can we tell if the content of a sequence is actually a string or merely a series of integers? I understand that from the language's point of view the two are exactly the same thing, but from the programmer's point of view they are not. Is there a way to store a metadata somewhere to indicate that "this is a string" and is meant to be interpreted and used as such?

The simple anwer is that there is no string (or character) data-type in Euphoria. There is no metadata in a sequence to turn it into a string data-type.

When integers are small ASCII values there is no way to distinguish a sequence used for numbers or for strings.

The console:display procedure has a heuristic that tries to print text as text, but it can't guess correctly all of the time.

There are UDT (user defined types) for strings and characters in the std/types.e library. Still, you can't definitively sort plain integers from integers used for characters.

Is this a problem? Most of the time it is not. If you put string values into sequence you simply remember to display them as text. This is something you will get used to.

The advantage of Euphoria is that the same sequence operations apply to all sequences making Euphoria easier to use overall.

Nevla said...

2) with regard to the use of named constants to reference the indexes of a sequence, I am assuming that there must be some conventional way of doing it, as this appears to be a basic need for handling sequences. From what I understand, the use of named constants for referencing indexes would be indispensable in the case of sequences whose structure is very dynamic - without constantly keeping track of the ever-changing indexes, the contents of a sequence would soon become inaccessible and the whole sequence would become useless. My question is: what's the best way to store, access and update named constants? I guess the end-result would resemble something like a lookup table. But how to implement it? (BTW, I am not asking about maps/associative arrays).

Indexes are integers; that is it.

There is no dictionary data-type in Euphoria.

Use enums if the sequence is unchanging.

So you do want to use std/map.e if you want dynamically to index an object using a string.

_tom

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

4. Re: Couple of questions about sequences

Nevla said...

1) ... how can we tell if the content of a sequence is actually a string or merely a series of integers?

You can't.

Nevla said...

Is there a way to store a metadata somewhere to indicate that "this is a string" and is meant to be interpreted and used as such?

Yes, but why bother. In practice, it is a rare problem.

Nevla said...

2)... what's the best way to store, access and update named constants?

I'm assuming you are talking about having names for indexes that are used to reference elements in a sequence. The easiest way is to use the enum method.

-- Example 
enum  
    CustID, 
    FirstName, 
    FamilyName, 
    Address, 
    CurrentBalance 
     

In the example above, CustID is assign the value 1 (default for enums), and each subsequent name is given the value of one more than the previous name.

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

5. Re: Couple of questions about sequences

DerekParnell said...
Nevla said...

2)... what's the best way to store, access and update named constants?

I'm assuming you are talking about having names for indexes that are used to reference elements in a sequence. The easiest way is to use the enum method.

-- Example 
enum  
    CustID, 
    FirstName, 
    FamilyName, 
    Address, 
    CurrentBalance 
     

In the example above, CustID is assign the value 1 (default for enums), and each subsequent name is given the value of one more than the previous name.

Actually I may have used the wrong word by saying "constant". What I meant was the case when named *variables* are used to reference the indexing of some data stored within a sequence whose structure is changing. Suppose the case where your data elements are initially assigned positions 1,2,3,4,5... within a sequence, but later on, because of the changes that take place within the sequence, their indexing position changes. So, in this case I would need to re-assign a new indexing value to each of the named variables, to reflect the new structure. Now, if the "coordinates" of the stored data within a sequence keep changing all the time, and there are several independent elements to keep track of, managing their referencing through named variables may become a challenge. My question was about the best way to maintain an auto-updating sort of lookup table via named variables, to cope with the the changes that take place in the sequence structure.

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

6. Re: Couple of questions about sequences

Same answer: an index is an integer.

Jiri Babor wrote Associative Lists (Tables) found in the Archive www.rapideuphoria.com/3tables.zip which may give you some ideas.

_tom

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

7. Re: Couple of questions about sequences

_tom said...

Same answer: an index is an integer.

That I understood. However my question is not being answered: how do I manage efficiently a lookup table that matches named variables with sequence indexes?

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

8. Re: Couple of questions about sequences

Nevla said...

However my question is not being answered: how do I manage efficiently a lookup table that matches named variables with sequence indexes?

I don't think I've ever done what you propose. If I decide to have a sequence be some sort of structured data, the structure is static. If I want it to be polymorphic, then the changes happen after the common data so that the enumerations of indices is still actually static.

Otherwise, I'd probably use a map.

Matt

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

9. Re: Couple of questions about sequences

Nevla said...
_tom said...

Same answer: an index is an integer.

That I understood. However my question is not being answered: how do I manage efficiently a lookup table that matches named variables with sequence indexes?

Sorry, may reply was terse.

"An index is an integer" means you really are limited to integer expressions to index items in a sequence.

If the sequence is static (meaning no changes in length or shape) then you can make a static list of variables (or constants) with assigned values to index items in a sequence. This is all fixed at the time you write your source-code.

The thing is Euphoria does not allow you to create or destroy variables during the execution of a program. That means you can not have a dynamic list of named variables that index a corresponding dynamic sequence (where the sequence length and shape changes.)

Using the items in a sequence as "variables" used to index another sequence is feasible. The resulting code will likely be messy.

To have the convenience of using some kind of name for indexing means you need a technique that allows you to use an a text string as a key to access a record. Look at the EDS (Euphoria Data Base) or std/map.e for this kind of service. The other example is 3tables.zip (from the RDS Archive) which shows you a simple way to synchronize a sequence of keys with a sequence of records.

Euphoria does not have a syntax trick that lets you use named variables for indexing and then have everything adjust automatically as the sequence changes length and shape.

If this does not answer your question then I need a sample of pseudo-code that demonstrates what you are trying to do.

_tom

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

10. Re: Couple of questions about sequences

Nevla said...

What I meant was the case when named *variables* are used to reference the indexing of some data stored within a sequence whose structure is changing. Suppose the case where your data elements are initially assigned positions 1,2,3,4,5... within a sequence, but later on, because of the changes that take place within the sequence, their indexing position changes. So, in this case I would need to re-assign a new indexing value to each of the named variables, to reflect the new structure.

You really want to do that?! Seriously?! Why? Can you describe a real-life example of where this is the best algorithm to use?

Nevla said...

Now, if the "coordinates" of the stored data within a sequence keep changing all the time, and there are several independent elements to keep track of, managing their referencing through named variables may become a challenge. My question was about the best way to maintain an auto-updating sort of lookup table via named variables, to cope with the the changes that take place in the sequence structure.

I can think of a few ways to do this, but the extra work involved doesn't seem to be worth the effort. I'd still like to see a scenario where this type of behaviour is necessary in a program.

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

11. Re: Couple of questions about sequences

_tom said...
Nevla said...
_tom said...

Same answer: an index is an integer.

That I understood. However my question is not being answered: how do I manage efficiently a lookup table that matches named variables with sequence indexes?

Sorry, may reply was terse.

"An index is an integer" means you really are limited to integer expressions to index items in a sequence.

If the sequence is static (meaning no changes in length or shape) then you can make a static list of variables (or constants) with assigned values to index items in a sequence. This is all fixed at the time you write your source-code.

The thing is Euphoria does not allow you to create or destroy variables during the execution of a program. That means you can not have a dynamic list of named variables that index a corresponding dynamic sequence (where the sequence length and shape changes.)

Using the items in a sequence as "variables" used to index another sequence is feasible. The resulting code will likely be messy.

To have the convenience of using some kind of name for indexing means you need a technique that allows you to use an a text string as a key to access a record. Look at the EDS (Euphoria Data Base) or std/map.e for this kind of service. The other example is 3tables.zip (from the RDS Archive) which shows you a simple way to synchronize a sequence of keys with a sequence of records.

Euphoria does not have a syntax trick that lets you use named variables for indexing and then have everything adjust automatically as the sequence changes length and shape.

If this does not answer your question then I need a sample of pseudo-code that demonstrates what you are trying to do.

_tom

Thanks, tom. Your last post perfectly answers my question.

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

12. Re: Couple of questions about sequences

DerekParnell said...
Nevla said...

What I meant was the case when named *variables* are used to reference the indexing of some data stored within a sequence whose structure is changing. Suppose the case where your data elements are initially assigned positions 1,2,3,4,5... within a sequence, but later on, because of the changes that take place within the sequence, their indexing position changes. So, in this case I would need to re-assign a new indexing value to each of the named variables, to reflect the new structure.

You really want to do that?! Seriously?! Why? Can you describe a real-life example of where this is the best algorithm to use?

OK, most probably you are right, Derek. You see, I was not asking the question having a real-life in example in mind. I am a total newbie at Euphoria and I was reading in the documentation that the structure of sequences keeps changing, because they are dynamic structures. So, I was just assuming that in order to store one's data into sequences one needs to have some fail-safe mechanism to make sure that the stored data does not become inaccessible when the sequence's structure changes. It was just a conjecture on my part. But now, thanks to the posts in this thread, I am realizing that the change in the sequence's structure, which is certainly something undesirable if you have to use sequences as records, will probably be a rare occurrence. So I guess I will not have to worry much about it.

Nevla said...

Now, if the "coordinates" of the stored data within a sequence keep changing all the time, and there are several independent elements to keep track of, managing their referencing through named variables may become a challenge. My question was about the best way to maintain an auto-updating sort of lookup table via named variables, to cope with the the changes that take place in the sequence structure.

DerekParnell said...

I can think of a few ways to do this, but the extra work involved doesn't seem to be worth the effort. I'd still like to see a scenario where this type of behaviour is necessary in a program.

Same as above. Coming to think about it, probably it would not be needed, after all.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu