1. String sequence characters and appending clarity

I know this is basic but I just need a little clarity on strings. My basic understanding was a string is simply a sequence. Given that, why cant I use append to concatenate two strings?

sequence s, ss 
s = "Hello " 
 
s &= "Fred"     --ok 
puts(1,s) 
 
ss = append(s, " Flintstone") --not ok, sequence found inside character string 
 
puts(1,ss) 
Thanks and apologies for such a basic question.

new topic     » topic index » view message » categorize

2. Re: String sequence characters and appending clarity

Anthill said...

I know this is basic but I just need a little clarity on strings. My basic understanding was a string is simply a sequence. Given that, why cant I use append to concatenate two strings?

sequence s, ss 
s = "Hello " 
 
s &= "Fred"     --ok 
puts(1,s) 
 
ss = append(s, " Flintstone") --not ok, sequence found inside character string 
 
puts(1,ss) 

Append adds the second parameter as an element to the end of the first parameter, so you'd have:

s = { 'F', 'r', 'e', 'd', " Flinstone"} 

For this operation, you need to use the concatenation operator, '&'.

Matt

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

3. Re: String sequence characters and appending clarity

A great debugging tool is pretty_print:

include std/pretty.e 
 
pretty_print(1, { "John", "Doe", { 18, 22, 19 } }, {3}) 

See: /search/results.wc?s=pretty_print for more details.

Jeremy

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

4. Re: String sequence characters and appending clarity

Anthill said...

... why cant I use append to concatenate two strings?

Because append and concatenate are not the same thing.

concatenate
Join together two objects. The resulting length is the sum of the objects' lengths.
append
Add the entire second object as one element to the first object. The resulting length is the length of the first object plus 1.

sequence A = {'a', 'b', 'c'} 
sequence B = {'d', 'e', 'f'} 
sequence C 
 
C = A & B --> {'a', 'b', 'c', 'd', 'e', 'f'}, length is now 6. 
-- You can see that each element of B has been 'added' to A  
 
C = append(A, B) --> {'a', 'b', 'c', {'d', 'e', 'f'}}, length is now 4. 
-- You can see that B has been added as a single element to A. 
 
new topic     » goto parent     » topic index » view message » categorize

5. Re: String sequence characters and appending clarity

Thanks for all of your comments. It does clarify. It was just a bit confusing at first to get my head around what happens with strings as they relate to sequences and the other languages I use.

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

6. Re: String sequence characters and appending clarity

Here is a table that I intend to add to the documentation:

Basic Sequence Operations

Starting with two sequences:

x = {1,2,3,4,5} 
y = {'a','b','c'} 

A tremendous wealth of sequence operations are possible:

Operation Example Check
index

? x[1] -- is 1
? x[$] -- is 5
minimum index is 1
maximum index is length
slice

? x[1..3] -- is {1,2,3}
? x[2..4] -- is {2,3,4}
? x[3..$] -- is {3.4.5}
concatenate

? x & y -- is {1,2,3,4,5,'a','b','c'}
lengths are added
splice

? splice(x,y,2} -- is {1,'a','b','c',3,4,5}
lengths are added
remove

? remove(x,2) -- is {1,3,4,5}
? remove(x,2,4) -- is {1,5}
length - length_slice
replace

? replace(x,y,2) -- is {1,'a','b','c',3,4,5}
? replace(x,y,2,4) -- is {1,'a','b','c',5}
length - length_slice + length_new
prepend

? prepend(x, y ) -- is {{'a','b','c'},1,2,3,4,5}
length + 1
insert

? insert(x,y,2} -- is {1,{'a','b','c'},2,3,4,5}
length + 1
append

? append(x,y) -- is {1,2,3,4,5,{'a','b','c'}}
length + 1
new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu