Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

setting values to blocks - need help

 [1/10] from: carlos:lorenz:gma:il at: 27-Apr-2009 16:12


Hi list, Need some help to understand this:
>> blk: copy []
== []
>> for i 1 3 1 [ append/only blk [] ]
== [ [] [] [] ]
>> append blk/1 "1"
== ["1"]
>> probe blk/1
["1"] == ["1"]
>> probe blk
[["1"] ["1"] ["1"]] == [["1"] ["1"] ["1"]] How come blk/2 and blk/3 were set to same value of blk/1 ??? Thanks -- Carlos Lorenz Unidade Lorenz Ltda www.revistaeletronica.com.br (11) 4034 1971 (11) 7100 1540 pessoal

 [2/10] from: compkarori:g:mail at: 28-Apr-2009 7:15


try append/only copy [] On Tue, Apr 28, 2009 at 7:12 AM, Carlos Lorenz <carlos.lorenz-gmail.com> wrote:
> Hi list, > Need some help to understand this:
<<quoted lines omitted: 21>>
> To unsubscribe from the list, just send an email to > lists at rebol.com with unsubscribe as the subject.
-- Graham Chiu http://www.synapsedirect.com Synapse - the use from anywhere EMR.

 [3/10] from: ptretter::hotmail::com at: 27-Apr-2009 14:44


Use 'copy on series to resolve that issue. Paul Tretter

 [4/10] from: tim-johnsons::web::com at: 27-Apr-2009 17:45


On Monday 27 April 2009, Carlos Lorenz wrote:
> How come blk/2 and blk/3 were set to same value of blk/1 ???
Under the hood: they are the same 'pointer' - i.e. referencing the same address in memory try 'array/initial - returns multiple memory references
>> blk: array/initial 3 []
== [[] [] []]
>> append blk/1 "1"
== ["1"]
>> blk
== [["1"] [] []] ;; HTH ;; tim

 [5/10] from: christian:ensel:gmx at: 28-Apr-2009 11:19


Hi Carlos, Tim already gave you the exact reasoning for what you're experiencing here, you can examine it for yourself:
>> loop 2 [append/only same: [] []]
== [[] []] Not only are these blocks equal:
>> equal? same/1 same/2
== true They are the same blocks, indeed:
>> same? same/1 same/2
== true That's why
>> append first same 1 same
== [[1] [1]] Whereas with fresh copies of the empty block
>> loop 2 [append/only equal: [] copy []]
== [[] []]
>> equal? equal/1 equal/2
== true
>> same? equal/1 equal/2
== false
>> append first equal 1 equal
== [[1] []] Cheers, Christian Tim Johnson schrieb:

 [6/10] from: semseddinm:bircom at: 28-Apr-2009 12:44


Strings and blocks are always references in Rebol. If you want different block for each of them, you should use copy command. It is same for strings also.

 [7/10] from: brock::kalef::innovapost::com at: 28-Apr-2009 12:18



 [8/10] from: tim-johnsons:web at: 28-Apr-2009 14:06


On Tuesday 28 April 2009, =DEemseddin Moldibi [ Bircom ] wrote:
> Strings and blocks are always references in Rebol. If you want different > block for each of them, you should use copy command. > It is same for strings also.
And that is not a bad thing, once you get used to it. For instance, if you were to assign a series value to a word in a function, rather than 'copy'ing the value to the word, the word then retains its value, for subsequent calls to that function. Like a static datatype in C. Can be useful at times. tim

 [9/10] from: tim-johnsons::web::com at: 28-Apr-2009 14:56


On Tuesday 28 April 2009, Tim Johnson wrote:
> And that is not a bad thing, once you get used to it. For instance, if > you were to assign a series value to a word in a function, rather than > 'copy'ing the value to the word, the word then retains its value, for > subsequent calls to that function. Like a static datatype in C. > Can be useful at times.
Sorry - left out an example. Example below: test: func[/local res][ count: [0] count/1: count/1 + 1 print rejoin["this function has been called " count/1 " times"] ]
>> loop 6[test]
this function has been called 1 times this function has been called 2 times this function has been called 3 times this function has been called 4 times this function has been called 5 times this function has been called 6 times ;; HTH tim

 [10/10] from: semseddinm::bircom::com at: 29-Apr-2009 9:35


Yes, it sometimes confuses beginners mind but actually it is a nice feature. Below example shows that feature very nice. It uses "count: [0]" instead of "count: 0" which is completly different.

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted