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

Newbie query on array indexing

 [1/18] from: philb:upnaway at: 23-Nov-2001 11:33


Hi Guys, My 3D Chess program uses arrays to specify the location of a piece on the 3D board. I wanted to set what piece is at a x,y,z wuth board/:x/:y/:z = #"Q" As pointed out this is not a valid syntax (at least in the current version of Rebol. The work around I was given was to declare my array with 1 extras dimension board: array/initial [5 5 5 1] #"." I can then use board/:x/:y/:z/1: = #"Q" Cheers Phil === Original Message === On Thu, Nov 22, 2001 at 10:14:04AM +0000, John R wrote:
> Hi Tim, > Thanks for your examples. Sorry if I was not too clear on last post - I did
<<quoted lines omitted: 27>>
> ** Near: (line 1) arr-test/:n: "AmendedEntry3" > >>
The short answer is: I believe that you can't do it. poke arr-test n "Amended Entry3" ; does the trick. I do agree that it would be a good idea to have a "gotchas" page. And I do agree that it really makes sense to me to be able to make assignments directly to path notation. Many of rebol features are the result of input from mailing list members. It is possible that some of the more advanced users or RT members could point out why this wasn't implemented. I would point out, that for me, when maintaining code that I haven't worked with in some time - poke arr-test ndx "AmendedEntry3" is more readable and pops out in my conciousness more so than arr-test/:n: "AmendedEntry3" but then 'poke has similar connotations in other languages. -- Tim Johnson <[tim--johnsons-web--com]> http://www.johnsons-web.com

 [2/18] from: ingo:2b1 at: 23-Nov-2001 7:42


Hi Ammon, I can tell you _what_ got set, but the _why_ is beyond me ...
>> one
** Script Error: one has no value ** Near: one
>> a: context [b: 'one] >> c: 'b
== b
>> query/clear system/words
== [end! unset! error! datatype! context! native! action! routine! op! function! object! struct! library! port! any-type! any-word!...
>> set a/:c 'two
== two
>> query system/words
== [one two]
>> one
== two
>>
Now we'll have a magician to step in and tell us, why 'one got set ... Kind regards, Ingo Once upon a time Ammon Johnson spoketh thus: <...>

 [3/18] from: brett:codeconscious at: 23-Nov-2001 22:14


> I can tell you _what_ got set, but the _why_ is beyond me ...
...
> >> one > == two > >>
Hello, my turn :) a: context [b: 'one] Here an object is created with a single instance variable "b" whose value is literal word "one". This object becomes the value of the word "a". b: 'b Then a new word "b" is given a value. The value of this new word is the literal word "b". set a/:b 'two Now Ammon is getting tricky. Let's break it down. Firstly it is pretty obvious we are setting a word to a value, this value being the literal word two . Volker has shown that the word that is being set is the word "one". So the mystery comes down to the evaluation of the path. Here are two quotes from the Core user guide: <quote> Paths are a collection of words and values delineated with forward slashes (/). Paths are used to navigate to or find something. The words and values of a path are called refinements, and they are combined to provide a means of navigating through a value or function." </quote> <quote> The words supplied as paths are symbolic and therefore unevaluated. This is necessary to allow the most intuitive form for object referencing. To use a word's reference, an explicit word value reference is required: city: 'Ukiah probe USA/CA/:city </quote> BTW, The word "reference" is used twice in that last sentence in two different ways. First to refer to the value of a word, second to talk about the Rebol expression (using a get-word syntax) that will retrieve the word's value. So back to the path Ammon has created Here it again: a/:b Imagine how this path is evaluated. First "a" is encountered it evaluates to an object. Then the next element of the path is evaluated. This is the ":b". Guess what :b will evaluate to.
>> :b
== b So it evaluates to the word "b". But the evalution of the path is not finished. This word b is used now as the selector (of an instance variable) into the object that was returned from the first element in the path. Thus the evalution now is equivalent too:
>> a/b
== one So the path evaluation is complete and has resulted in a value of the word one . This value now becomes a parameter to the set function and so now we have something equivalent to: set 'one 'two HTH Brett.

 [4/18] from: ingo:2b1 at: 23-Nov-2001 12:47


<slaps his head> Thanks Brett, Once upon a time Brett Handley spoketh thus:
> So the path evaluation is complete and has resulted in a value of the word > "one". This value now becomes a parameter > to the set function and so now we have something equivalent to: > > set 'one 'two
<..> And I _did_ think: "Maybe I should try with an integer instead of one ..."
>> a: context [b: 1] >> c: 'b
== b
>> set a/:c 'test
** Script Error: set expected word argument of type: any-word block ** Near: set a/:c 'test Would 'v made it clear in an instant ... thanks, Ingo

 [5/18] from: greggirwin:mindspring at: 23-Nov-2001 11:01


Excellent post Brett!

 [6/18] from: greggirwin:mindspring at: 23-Nov-2001 11:01


Hi Phil, << The work around I was given was to declare my array with 1 extras dimension board: array/initial [5 5 5 1] #"." I can then use board/:x/:y/:z/1: = #"Q" >> Now *that's* clever! --Gregg

 [7/18] from: jrdrp::blueyonder::co::uk at: 23-Nov-2001 21:28


Hi Phil, Just what I was wanting to do orginally with my arrays! I thought it should be do-able with this syntax. Thanks John

 [8/18] from: ammonjohnson:yah:oo at: 23-Nov-2001 17:12


I had to look at it a couple of times to see just what was happening ;) I was looking for the 'in function, the only problem with it is it only works with objects maybe, some day, I will write a version for blocks. ;) Enjoy!! Ammon

 [9/18] from: jrdrp:blueyonder at: 21-Nov-2001 22:37


What is best method to reference array elements using a variable subscript value. E.g. I was trying to stuff values into a 100 entry array based on a computed subsript like this:- arrcodes: array 100 "" subscript: (=computed value 1 to 100) arrcode/[subscript]: "Code stored for nn" John

 [10/18] from: greggirwin:mindspring at: 21-Nov-2001 16:22


Hi John, << What is best method to reference array elements using a variable subscript value. E.g. I was trying to stuff values into a 100 entry array based on a computed subsript like this:- arrcodes: array 100 "" subscript: (=computed value 1 to 100) arrcode/[subscript]: "Code stored for nn" >> How about: change at arrcodes subscript "Code stored for nn" I don't know if that's the best method, but it works. :) --Gregg

 [11/18] from: brett:codeconscious at: 22-Nov-2001 10:43


Hi, First off creating the array "example-array" is done like this: example-array: array 10 This gives you 10 NONEs in a block. To get an array of 10 zeroes use: example-array: array/initial 10 0 And a side issue (heads up). You are going to get caught if you think you will get ten different strings with this - you actually get ten references to the same string: example-array: array/initial 10 "" insert example-array/1 "test" probe example-array Now for indexing into the array. If I use a subscript "indx" here are some methods to access the array: example-array: array/initial 10 0 poke example-array indx 42 example-array/:indx pick example-array indx And as Gregg showed you can use the normal series functions. Because actually ARRAY is a function that returns a block.
>> type? array 3
== block! Brett.

 [12/18] from: jrdrp:blueyonder at: 22-Nov-2001 0:41


Greg, Brett, Thanks for your suggestions and advice. While Rebol is certainly powerful it seems to have plenty of quirks waiting to trip the unwary - maybe it gets better the more you get into it! I still could not get the following type of array element assignment reference to work (this was my original approach):- ; define array example-array: array/initial 10 "" ; define index idx: 3 ; try to set element at offset defined by index value example-array/:n : "data" Different syntax required? Thanks John

 [13/18] from: greggirwin:mindspring at: 21-Nov-2001 22:23


Hi John, << While Rebol is certainly powerful it seems to have plenty of quirks waiting to trip the unwary - maybe it gets better the more you get into it!
>>
I think it's like learning to ride a motorcycle. At first you take it slow because you know you don't know what you're doing. After a while, you *think* you know what you're doing, but it isn't for quite some time that the process becomes part of your psyche and you can just "do it" without thinking about it. Only with REBOL it more than "1 down - 4 up". :) << I still could not get the following type of array element assignment reference to work (this was my original approach):- ; define array example-array: array/initial 10 "" ; define index idx: 3 ; try to set element at offset defined by index value example-array/:n : "data" Different syntax required? >> You can use either 'pick, as Brett suggested, or 'change as I suggested. They differ in what they return to you, so that's something to be aware of. Look at help on each of them to see what I mean. If you're reading the value, example-array/:n will work, but you can't set the value with that syntax. With REBOL you'll find that you need to think about things differently. When you can let go of how other languages work (I'm still working on that, BTW) it will get easier and you'll write more REBOLish code which, at least for me, is very different from what I would write in another language. HTH! --Gregg

 [14/18] from: tim:johnsons-web at: 21-Nov-2001 19:25


Hi John
> Greg, Brett, > > Thanks for your suggestions and advice. While Rebol is certainly powerful it > seems to have plenty of quirks waiting to trip the unwary - maybe it gets > better the more you get into it!
I will guarantee that it does get better.... Here's a couple of observations All programming languages have "hidden quirks". Having written in about 10 of them, I find I create my own hidden quirks by assuming the logic of the language rathering than taking the time to understand it as well as I should. I find that I understand rebol better if I think of it as a C program (which I believe the binary IS), but don't think of the syntax as anything but possibilities....
> I still could not get the following type of array element assignment > reference to work (this was my original approach):-
<<quoted lines omitted: 4>>
> ; try to set element at offset defined by index value > example-array/:n : "data"
Why not try the following exercise, John..
>> test: make block! 25
== [] ; there are maybe better ways to do this, but this creates unique "pointers" ; into 'test
>> loop 25[append test copy ""]
; looks the same, but each element is different ; reference Brett's email on this subject == ["" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""]
>> append test/20 "line 20"
== "line 20"
>> test
== ["" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "line 20" "" "" "" "" ""]
>> poke test 15 "this is element 15"
== ["" "" "" "" "" "" "" "" "" "" "" "" "" "" "this is element 15" "" "" "" "" "line 20" "" "" "" "" ""]
> Different syntax required?
Here's another exercise: Prepare to be edified! :>)
>> test: make block! 25
== []
>> loop 25[append test copy ""] >> same? test/2 test/22
== false ; COMPARE THIS TO THE NEXT EXAMPLE
>> arrcodes: array/initial 25 ""
== ["" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""]
>> same? arrcodes/2 arrcodes/22
== true ;VERY IMPORTANT DISTINCTION
> Thanks > John
One method that works well in "harnessing" the power of this mailing list is to either: copy and paste a contiguous interpreter session into your email. OR Send a complete script. HTH -- Tim Johnson <[tim--johnsons-web--com]> http://www.johnsons-web.com

 [15/18] from: jrdrp:blueyonder at: 22-Nov-2001 10:14


Hi Tim, Thanks for your examples. Sorry if I was not too clear on last post - I did already check out and understand (I hope) all techniques mentioned by Greg and Brett and noted the initialisation behaviour with strings, pointers etc. IMO it would be nice if "unusual" behaviour like this were highlighted in the Rebol documentation. My outstanding query was trying to assign a value to an array element using path syntax with subscript variable. Console session extract below. Thanks John
>> arr-test: array 10 ""
== ""
>> loop 10 [append arr-test copy ""]
== [none none none none none none none none none none "" "" "" "" "" "" "" "" ""]
>> arr-test/1: "Entry1"
== ["Entry1" none none none none none none none none none "" "" "" "" "" "" "" "" ""]
>> arr-test/3: "Entry3"
== ["Entry1" none "Entry3" none none none none none none none "" "" "" "" "" "" " ]
>> n: 3
== 3
>> print arr-test/:n
Entry3
>> arr-test/:n:
AmendedEntry3" ** Syntax Error: Invalid word -- :n: ** Near: (line 1) arr-test/:n: "AmendedEntry3"

 [16/18] from: brett:codeconscious at: 22-Nov-2001 23:50


Hi John,
> My outstanding query was trying to assign a value to an array element
using
> path syntax with subscript variable. Console session extract below.
I believe the really short answer to your query is "no". It may be helpful to re-read the section on paths in the Core user guide. I did and I'm glad I did. Brett.

 [17/18] from: tim:johnsons-web at: 22-Nov-2001 9:01


On Thu, Nov 22, 2001 at 10:14:04AM +0000, John R wrote:
> Hi Tim, > Thanks for your examples. Sorry if I was not too clear on last post - I did
<<quoted lines omitted: 27>>
> ** Near: (line 1) arr-test/:n: "AmendedEntry3" > >>
The short answer is: I believe that you can't do it. poke arr-test n "Amended Entry3" ; does the trick. I do agree that it would be a good idea to have a "gotchas" page. And I do agree that it really makes sense to me to be able to make assignments directly to path notation. Many of rebol features are the result of input from mailing list members. It is possible that some of the more advanced users or RT members could point out why this wasn't implemented. I would point out, that for me, when maintaining code that I haven't worked with in some time - poke arr-test ndx "AmendedEntry3" is more readable and pops out in my conciousness more so than arr-test/:n: "AmendedEntry3" but then 'poke has similar connotations in other languages. -- Tim Johnson <[tim--johnsons-web--com]> http://www.johnsons-web.com

 [18/18] from: ammonjohnson::yahoo at: 22-Nov-2001 17:31


The problem is NOT with paths! It is the fact that you are trying to GET a word & SET the *same* word, try this:
>> :a: >> :a:
** Syntax Error: Invalid word -- :a: ** Near: (line 1) :a: You see it is the word not the path! Now I was going to give you a miracle cure, but that seems beyond me. Can anyone explain the following:
>> a: context [b: 'one] >> b: 'b
== b
>> set a/:b 'two
== two
>> a/b
== one
>> b
== b What got set? Any ideas? It appears to me that nothing got set, what do you see? HTH!! Ammon

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