Re: Append
- Posted by mindwalker Sep 23, 2014
- 1572 views
buzzo said...
Need help understanding append
object pc = {{},{}} pc[1] = "1234" pc[2] = "2345" -- prints 2 puts(1,"len pc = " & to_string(length(pc)) & "\n") for x = 1 to length(pc) do -- prints 1234, then 2345 puts(1, to_string(pc[x]) & "\n") end for pc = append(pc,pc) -- print 3; i thought it would be 2 puts(1,"len pc after append = " & to_string(length(pc)) & "\n") pc[2][1] = "3456" pc[2][2] = "4567" for x = 1 to length(pc) do for y = 1 to 2 do -- prints 1,2 then 3456,4567, then 1234, 2345 -- I had planned on it printing 1234, 2345 then 3456, 4567 puts(1,pc[x][y] & "\n") end for end for
Historically the append(seq, obj) function has added one object to the tail end of the sequence. Thus the append function always adds one and only one to the length of the sequence.
So in your code the following line produces the sequence shown below it (Note that the entire original pc sequence is appended to the end.):
pc = append(pc,pc) { {1,2,3,4}, {2,3,4,5}, {{1,2,3,4}, {2,3,4,5}} }
Going forward I believe it was decided to modify append to append(obj, obj) where if the first object is an atom/integer the function will turn that into a sequence which contains the single value and then it will append the second object to that sequence.