1. Using insert()

Goal: To create a file to store an extracted selected subtree having appropriate name. Each descriptor is 12 characters long.

integer node_descriptor_loc 
sequence UCTree -- entire file of descriptors 
sequence extract_path 
 
      node_descriptor = UCTree[node_descriptor_loc .. node_descriptor_loc + 11]  
      extract_path = insert("UCT.dat", node_descriptor, 4) 
      open({extract_path}, "wb") 



Problem: 'open' statement results in error: "character string contains a sequence" (refers to extract_path)

Tried using quotes "node_descriptor"
No luck

new topic     » topic index » view message » categorize

2. Re: Using insert()

You should learn to place lots (and lots) of output statements in your code when you get stuck.

integer node_descriptor_loc  
sequence UCTree -- entire file of descriptors  
sequence extract_path  
  
      node_descriptor = UCTree[node_descriptor_loc .. node_descriptor_loc + 11]   
puts(1, node_descriptor ) puts(1, "\n" ) 
 
      extract_path = insert("UCT.dat", node_descriptor, 4)  
puts(1, extract_path ) puts(1, "\n" ) 
 
puts(1, {extract_path} ) 
 
      open({extract_path}, "wb")  
 

Let us say extract_path is a string sequence.

Then { extract_path } is a sequence inside a sequence--which is bad for opening a file.

_tom

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

3. Re: Using insert()

Sorry I'm so dense, Tom, but I still don't see the way around my problem. Without a doubt, the {} around output_path did not help matters, but neither does removing them. I created a sequence inside a character string. My bad! But how do I assemble the output path otherwise? with puts statements?

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

4. Re: Using insert()

node_descriptor is a string of 12 characters. When I access them via a slicing operation, they turn up as a sequence. OK, a character string is a sequence, but when I attempt to insert them into an existing character string, I have created a sequence within a string ??? What is the solution"

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

5. Re: Using insert()

-- splice vs insert 
 
 
sequence s = "Hello friend" 
 
sequence t = "good " 
 
-- a "splice" creates a smooth joint in a string (or rope, "nautical" line ) 
 
puts(1, splice( s, t, 7 ) ) 
 
-- an "insert" creates a rough knot in a string (or rope, "nautical" line ) 
 
-- puts(1, insert( s, t, 7 ) ) 
--> error, sequence found in a string 
 
include std/console.e 
display( insert( s, t, 7 )) 
 
/* 

Hello good friend 
 
{ 
  72, 
  101, 
  108, 
  108, 
  111, 
  32, 
  "good ", 
  102, 
  114, 
  105, 
  101, 
  110, 
  100 
} 
*/ 

Things in Euphoria often come down to two choices. Guess wrong, and it is probably the second choice.

insert) and splice) go together as a pair

_tom

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

6. Re: Using insert()

original post should have included
atom extract_fn
extract_fn = open( ......

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

7. Re: Using insert()

You would be well served by creating a routine that will execute with sample data. Try the following to see how splice works.

integer node_descriptor_loc 
node_descriptor_loc = 1  
sequence UCTree -- entire file of descriptors  
sequence extract_path  
sequence node_descriptor 
UCTree = "PREPOSTERIOR" 
puts(1, UCTree) 	 
puts(1, "\n") 
    node_descriptor = UCTree[node_descriptor_loc .. node_descriptor_loc + 11]   
puts(1, node_descriptor) 	 
puts(1, "\n") 
include std/console.e 
display(node_descriptor&"2") 
    extract_path = splice(node_descriptor, "tst.dat", 13) 
puts(1, extract_path) 	 
puts(1, "\n") 
    atom extract_fn 
    extract_fn = open(extract_path, "wb")  
    close(extract_fn) 
new topic     » goto parent     » topic index » view message » categorize

8. Re: Using insert()

alrobnett said...

Goal: To create a file to store an extracted selected subtree having appropriate name. Each descriptor is 12 characters long.

integer node_descriptor_loc 
sequence UCTree -- entire file of descriptors 
sequence extract_path 
 
      node_descriptor = UCTree[node_descriptor_loc .. node_descriptor_loc + 11]  
      extract_path = insert("UCT.dat", node_descriptor, 4) 
      open({extract_path}, "wb") 



Problem: 'open' statement results in error: "character string contains a sequence" (refers to extract_path)

Tried using quotes "node_descriptor"
No luck

When you get the error sequence inside character string, you've always put a sequence in a sequence. So what you have done is inserted the sequence "UCT.dat" into the sequence node descriptor. Lets say node_descriptor is "ThisIsMyNodeDescriptor", then what you have created is "This"UCT.dat"MyNodeDescriptor" - or to look at it another way

T                 -one character is a sequence of 1 
h 
i 
s 
UCT.dat           -this is the sequnce you inserted 
M 
y 
N 
and so on 

I have done this so many times, believe me you are not alone. Splice and insert are very good sequence manipulators, but sometimes it makes the code clearer if you simplify even further. First create you individual bits of the string you want to create, and then & them together. so, for instance, using the above example

sequence str1, str2, str3, node_descriptor 
 
node_descriptor = "ThisIsMyNodeDescriptor" 
str1 = node_descriptor[1..4] 
str2 = node_descriptor[5..$] 
 
node_descriptor = str1 & "UCT.dat" & str2 
 
--I often do this in puts to add a line feed 
puts(1, node_descriptor & "\n" 
 

Cheers

Chris

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

9. Re: Using insert()

Thanks, Tom and Chris. The problem was solved by simply changing the word 'insert' to the word 'splice' with no change to the parameters. Some day I would like to understand the principles involved; a string is a sequence except when it isn't a sequence. One cannot insert a sequence into a string, but can splice a sequence into a string. I don't doubt that there is a sound logic, but is isn't obvious to me.

It is immaterial, but the purpose of this particular function is maintenance and modification of a binary tree consisting of 32,767 elements of 12 characters each (file size 393,216). The function in question is extracting a subtree from the 32,767-element binary tree when it is discovered that the subtree is in the wrong place. It can then be re-inserted (using the subtree file just created) into a more proper location in the main tree file. Since the first parameter of both 'insert' and 'splice' is the target string, I was inserting the selected twelve-character element into the string, "UCT.dat' at position 4, not the other way around. The result is 'UCT(twelve chars).dat'. It works beautifully!

Thanks again for all of the wonderful help. Euphoria Rocks!
And so do the folks in its forum!
Allen

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

10. Re: Using insert()

There is a short answer...

string | sequence

A string is "a flat sequence of atoms." Each atom being an integer code for plain text or UTF encoding.

If you insert something other than an atom (as a valid code) into a string-- you no longer have a valid string.

str = "●●●●●●●●" 
    -- a string 
 
str = insert(str, {"bad"}, 3 ) 
? str 
    --> { ● , ● , {"bad"}, ● , ● , ● , ● , ● , ● } 
    -- no longer a string 

And a long answer follows

_tom

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

11. Re: Using insert()

o☰

o

OpenEuphoria means atom|sequence.

┌───┐ 
│o☰ │ 
└───┘ 


Legend: ○ atom, ☰ sequence, □ object.

An atom ● symbol is any single number value.
A sequence ☰ symbol is any sequence { ■ , ■ } or string "●●".
An object ■ symbol is any value ● or ☰.

-- typical objects 
{ ■ , ■ , ■ , ■ }   { □ , □ , ☰ }           "●●●" 
 
3.1  { 1,  2,  3,  4 }   {  "hi", 5, "hello" }   "cat" 

Ideas Grow in Pairs

Ideas, values, and actions in oE|Phix often come as pairs:

atom | sequence

item | slice

remove | insert

introduce | join

append | concatenate

object | { object }

...

If write code featuring one idea and you get an error or unexpected result--you only have to check on the opposite pair. You are always 50% or 100% correct in Euphoria thinking.

Replace | Remove

An assignment action "gives an object its value." Use an assignment set the original value, or to change the value of an object. The result of an assignment is to replace one value with another.

atom a = ●    -- initial value 
 
     a = ○    -- new assignment, new value 
 
sequence s = {} 
         s = { □ , □ , ☰ } 

An item is "one object in a sequence list."

-- third item is □ 
 
{ ■ , ■ , □ , ■ } 

A slice is "a sub-sequence within a sequence."

-- original sequence 
{ □ , ▧ , ■ , ■ , ■  } 

permits many slicing choices

 
-- a few possible slices 
{ }  { }  
 
{ □ , ▧ } { ▧ , ■ }  
 
{  □ , ▧ , ■ }  { ■ , ■ , ■  } 
 
... 
 
{ □ , ▧ , ■ , ■ , ■   } 

An assginment in oE can not destroy or create top-level items in a sequence list. The length of a sequence does not change after any assignment. You can assign any value to a single item in a sequence. You can assign a slice to a slice, as long as their lengths are identical.

The original oE slice and the replacement oE slice must have the same length.

sequence s = { ■ ,  ■ , ■ ,     ■ ,       ■  } 
        s[1] = ▧  
 
       s[2..3] =  { □ , □  } 
 
            s[3..4] = { "●●●", "○○○" } 
 
? s 
--> { ▧ , □ , "●●●", "○○○" , ■ } 
sequence s = { 1,2,3,4,5 } 
s[1] = 10 
s[2..3] = { 200, 300 } 
s[3..4] = { "cat", "dog" } 
? s 
 
--> {10,200,"cat","dog",5} 

oE is strict about the conservation of items when you make an assignment to a sequence.

-- oE will issue error messages 
 
sequence s  
 
s = {1,2,3,4,5} 
s[1..3] = { 100, 200, 300, 400 } 
-- error 
-- lengths do not match on assignment to slice (3 != 4) 
 
s = {1,2,3,4,5} 
s[1..5] = {} 
? s 
-- error 
-- lengths do not match on assignment to slice (5 != 0) 

But, Phix will allow these assignments.

Remove

A remove "destroys an item or slice from a sequence." The new length is now shorter by the one (if item removed) or by the length of the removed slice.

An item is removed:

s = { ■ , □ , ■ , ■ } 
 
s = remove(s, 2 ) 
? s 
    --> { ■ , ■ , ■ } 

A slice is removed:

s = { ■ , □ , □ , ■ } 
s = remove(s, 2, 3 ) 
? s 
  --> { ■ , ■ } 

Warning: object | { object }

An object ■ is NOT THE SAME as a sequence with one object { ■ }. An atom ● and sequence { ● } are clearly different.

The trick is, after a concatenation of an atom ● or sequence { ● } the result looks the same. You can not tell what the starting object was.

 
s = { ■ , ■ , ■ , ■ } 
s = s & ○ 
? s 
    --> { ■ , ■ , ■ , ■ , ○ } 
     
s = { ■ , ■ , ■ , ■ } 
s = s &  { }  
? s 
    --> { ■ , ■ , ■ , ■ , ○ } 
sequence s 
 
s = {1,2,3} 
s = s & 999 
? s 
    --> {1,2,3, 999 } 
     
s = {1,2,3} 
s = s & {999} 
? s 
    --> {1,2,3, 999 } 
new topic     » goto parent     » topic index » view message » categorize

12. Re: Using insert()

Append | Concatenate

An append "introduces one new item to the end of a sequence list." An append increases the length of a sequence by one.

s = { ■ , ■ , ■ , ■ } 
 
            -- introducing action 
s = append(s, □ } 
? s 
-->   { ■ , ■ , ■ , ■ , □ } 
sequence s  
 
s = {1,2,3,4} 
s = append(s, 99 ) 
? s 
 --> {1,2,3,4, 99 }   -- one item introduced 
 
s = {1,2,3,4} 
s = append(s, {99} ) 
? s 
 --> {1,2,3,4, {99} }  -- one item introduced 

A prepend "introduces one new item to the start of a sequence list." A prepend works at the start of the sequence list (it is like an append).

A concatenation "joins two objects together." After joining, the final length is equal to the sum of the two original lengths.

s1 = { ■ , ■ , ■ , ■ } 
s2 = { □ , □  } 
 
 
            -- joining action 
? s1 & s2 
--> { ■ , ■ , ■ , ■ ,  □ , □  } 
sequence s1, s2 
 
s1 = {1,2,3,4} 
s2 = {50,60} 
? s1 & s2 
--> {1,2,3,4,50,60} 

Insert | Splice

An insert "introduces one item into a sequence." You can introduce an item at the head of a sequence (use insert or prepend), within a sequence (insert), or at the tail of a sequence (insert or append). The prepend|append actions are very common so they get their own functions. The length of a sequence after an insert increases by one.

s = { ■ , ■ , ■ , ■ } 
s = insert(s, □ , 2 ) 
? s 
  --> { ■ , □ , ■ , ■ , ■ } 
 
 
s = { ■ , ■ , ■ , ■ } 
s = insert(s, 5 ) 
? s 
    --> { ■ , ■ , ■ , ■ , □ } 
 
 
 
    -- an append is the same as an insert to length+1 
s = { ■ , ■ , ■ , ■ } 
s = append(s, □ ) 
? s 
    --> { ■ , ■ , ■ , ■ , □ } 
sequence s 
 
s = {1,2,3,4} 
s = insert(s, 999, 2 ) 
? s 
    --> {1, 999, 2,3,4} 
 
s = {1,2,3,4} 
s = insert(s, 999, 5 ) 
? s 
    --> {1,2,3,4, 999 } 
     
s = {1,2,3,4} 
s = insert(s, 999, 1 ) 
? s 
    --> { 999, 1,2,3,4} 

A splice "joins the ends of objects together." Sum the lengths of the spliced objects to get the length of the new sequence.

s = { ■ , ■ , ■ , ■ } 
s = splice(s, { □ , ▧  }, 2 ) 
? s 
    --> { ■ , □ , ▧ , ■ , ■ , ■ } 
 
 
s = { ■ , ■ , ■ , ■ } 
s = splice(s, { □ , ▧  }, 4 ) 
? s 
    --> { ■ , ■ , ■ , ■ , □ , ▧  } 
     
 
-- a splice at the tail is the same as concatenation & 
 
s = { ■ , ■ , ■ , ■ } 
s = { ■ , ■ , ■ , ■ } & { □ , ▧  } 
? s 
    --> { ■ , ■ , ■ , ■ , □ , ▧  } 
sequence s 
 
s = {1,2,3,4} 
s = splice(s, {66,99}, 2 ) 
? s 
    --> {1,  66,99,  2,3,4 } 
 
s = {1,2,3,4} 
s = splice(s, {66,99}, 5 ) 
? s 
    --> {1,2,3,4, 66,99 } 
 
s = {1,2,3,4} 
s = s & {66,99} 
? s 
    --> {1,2,3,4, 66,99 } 

string | sequence

A string is "a flat sequence of atoms." Each atom being an integer code for plain text or UTF encoding.

If you insert something other than an atom (as a valid code) into a string-- you no longer have a valid string.

str = "●●●●●●●●" 
    -- a string 
 
str = insert(str, {"bad"}, 3 ) 
? str 
    --> { ● , ● , {"bad"}, ● , ● , ● , ● , ● , ● } 
    -- no longer a string 
new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu