[Newbies] Re: combining arrays.....?
Bert Freudenberg
bert at freudenbergs.de
Tue May 4 13:24:50 UTC 2010
On 03.05.2010, at 19:38, noobie wrote:
>
>
> I cant get the comma thing to work.....what I was trying is something like
>
> combine: arg
> |Array1 i|
> Array1 := Array new: (self size + arg size).
>
> i:= (self size + arg size).
> [i>0] whileTrue:
>
> [
> (i=nil) ifTrue: [Array1 at: i put:(self at i).]
> ifFalse: [Array1 at: i put:(arg at i). i:= i+1.]
>
> ].
>
> But I cant seem to get it working??
I'd use a stream, and #with:do:, much simpler:
Array streamContents: [:stream|
#(1 2 3) with: #(a b c) do: [:x :y |
stream nextPut: x; nextPut: y]]
A good rule of thumb is that whenever you find yourself muddling with indices in Squeak, there is a better way. You can do that too, but it is considered low-level and bad style because it's much easier to get wrong:
| x y result |
x := #(1 2 3).
y := #(a b c).
result := Array new: x size * 2.
1 to: x size do: [:i |
result at: i * 2 - 1 put: (x at: i).
result at: i * 2 put: (y at: i)].
^result
- Bert -
More information about the Beginners
mailing list