HI
doing the pharo tutorial and I wanted to know how pharo viewed the dynamic array at index 3(seems smalltalk starts at 1 not 0) From the xample simple enough { (2+3) . (6*6) . 'hello', ' Stef'} size. 3 from the strings example we found index by 'ProfStef' at: 1. $P so I thought { (2+3) . (6*6) . 'hello', ' Stef'} at 3. would let me know what the array was evaluated to after execution but it doesn't. Is it confused because it doesn't know whether I want the third array element or the third character? Led me to wonder the correct way if doing this, so that I could evaluate array 1 * 2. { (2+3) . (6*6) . 'hello', ' Stef'} at 1 * at 2. 180 Cheers |
I do not really understand your problem
Now in Smalltalk #( ) is a literal array {} a dynamic the expressions put in the first are not evaluated while in the second yes. Stef On Dec 16, 2010, at 2:48 PM, flebber wrote: > > HI > > doing the pharo tutorial and I wanted to know how pharo viewed the dynamic > array at index 3(seems smalltalk starts at 1 not 0) > > From the xample simple enough > { (2+3) . (6*6) . 'hello', ' Stef'} size. 3 > > from the strings example we found index by > > 'ProfStef' at: 1. $P > > so I thought { (2+3) . (6*6) . 'hello', ' Stef'} at 3. would let me know > what the array was evaluated to after execution but it doesn't. Is it > confused because it doesn't know whether I want the third array element or > the third character? > > Led me to wonder the correct way if doing this, so that I could evaluate > array 1 * 2. > > { (2+3) . (6*6) . 'hello', ' Stef'} at 1 * at 2. 180 > > > Cheers > > -- > View this message in context: http://forum.world.st/Dynamic-Arrays-view-index-at-execution-tp3090891p3090891.html > Sent from the Pharo Smalltalk mailing list archive at Nabble.com. > |
In reply to this post by flebber
Den 16.12.2010 14:48, skrev flebber: > HI > > doing the pharo tutorial and I wanted to know how pharo viewed the dynamic > array at index 3(seems smalltalk starts at 1 not 0) > > >From the xample simple enough > { (2+3) . (6*6) . 'hello', ' Stef'} size. 3 > > from the strings example we found index by > > 'ProfStef' at: 1. $P > > so I thought { (2+3) . (6*6) . 'hello', ' Stef'} at 3. would let me know > what the array was evaluated to after execution but it doesn't. Is it > confused because it doesn't know whether I want the third array element or > the third character? > > Led me to wonder the correct way if doing this, so that I could evaluate > array 1 * 2. > > { (2+3) . (6*6) . 'hello', ' Stef'} at 1 * at 2. 180 > > > Cheers What do you mean? The dynamic array is created with three elements, 5 (2+3) at index 1, 36 (6*6) at index 2, and 'hello Stef' ('hello', ' Stef') at index 3. at: 3 thus returns the string. at 3 is invalid, as it is basically 2 method calls, array does not understand a message called #at, and #3 is an invalid selector anyways. For your second expression, writing ({ (2+3) . (6*6) . 'hello', ' Stef'} at: 1) * (at: 2) would not work, as you're missing the receiver for the second at: call. you'd need to do something like: array := { (2+3) . (6*6) . 'hello', ' Stef'}. (array at: 1) * (array at: 2). 180 Cheers, Henry |
Yes so simply if at 1 returns the first element of a string, what returns the third element of dynamic array, do you have to assign a varaible for the array to be able to retrieve elements? |
As Henrik pointed out, "foo at 1" won't even compile, let alone return anything, because "1" isn't a valid name for a method, and "at" (without the ":") doesn't take any arguments.
To get the first object in an indexable collection, be it String, Array or whatever, you use "foo at: 1". To get the third object, you use "foo at: 3". It really is as simple as that. I usually say that with Smalltalk, if what you're doing feels complicated or hard, then you're doing it wrong. Here, you were simply missing the ":". -- Cheers, Peter. On 16 dec 2010, at 23:22, flebber <[hidden email]> wrote: > > > Henrik Sperre Johansen wrote: >> >> >> >> Den 16.12.2010 14:48, skrev flebber: >>> HI >>> >>> doing the pharo tutorial and I wanted to know how pharo viewed the >>> dynamic >>> array at index 3(seems smalltalk starts at 1 not 0) >>> >>>> From the xample simple enough >>> { (2+3) . (6*6) . 'hello', ' Stef'} size. 3 >>> >>> from the strings example we found index by >>> >>> 'ProfStef' at: 1. $P >>> >>> so I thought { (2+3) . (6*6) . 'hello', ' Stef'} at 3. would let me know >>> what the array was evaluated to after execution but it doesn't. Is it >>> confused because it doesn't know whether I want the third array element >>> or >>> the third character? >>> >>> Led me to wonder the correct way if doing this, so that I could evaluate >>> array 1 * 2. >>> >>> { (2+3) . (6*6) . 'hello', ' Stef'} at 1 * at 2. 180 >>> >>> >>> Cheers >> >> What do you mean? >> The dynamic array is created with three elements, >> 5 (2+3) at index 1, >> 36 (6*6) at index 2, and >> 'hello Stef' ('hello', ' Stef') at index 3. >> >> at: 3 thus returns the string. >> at 3 is invalid, as it is basically 2 method calls, array does not >> understand a message called #at, and #3 is an invalid selector anyways. >> >> For your second expression, writing >> ({ (2+3) . (6*6) . 'hello', ' Stef'} at: 1) * (at: 2) would not work, >> as you're missing the receiver for the second at: call. >> you'd need to do something like: >> array := { (2+3) . (6*6) . 'hello', ' Stef'}. >> (array at: 1) * (array at: 2). 180 >> >> Cheers, >> Henry >> >> >> >> >> > Yes so simply if at 1 returns the first element of a string, what returns > the third element of dynamic array, do you have to assign a varaible for the > array to be able to retrieve elements? > -- > View this message in context: http://forum.world.st/Dynamic-Arrays-view-index-at-execution-tp3090891p3091774.html > Sent from the Pharo Smalltalk mailing list archive at Nabble.com. > |
Free forum by Nabble | Edit this page |