Re: combining arrays.....?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: combining arrays.....?

sergio_101-2
> you are correct....it returns #(1 2 3 4 5 #a #b #c)......why is it returning
> the abc with a # in front?
>

the '#' denotes symbols..(someone should step in here) but i am
guessing that it is storing it as a symbol rather than a character?

> Also, im trying to obtain #(1 a 2 b 3 c 4 5) basically.....any ideas?

oh!

so you are looking to sort of "shuffle" the two arrays into each other?


--
----
peace,
sergio
photographer, journalist, visionary

http://www.CodingForHire.com
http://www.coffee-black.com
http://www.painlessfrugality.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: combining arrays.....?

Bert Freudenberg
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 -


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: combining arrays.....?

hernanmd
Hi,

#with:do: require both collections of equal size, but in your example
you have two collection of different sizes, and as nobody wrote a
solution for that case, here are some other ways to do it

| col1 col2 |
col1 := #(1 2 3 4 5). col2 := #($a $b $c).
result := OrderedCollection new.
1 to: ( col1 size min: col2 size )
        do: [: idx | result add: ( col1 at: idx ); add: ( col2 at: idx )].
result addAll: (
        col1 size > col2 size
                ifTrue: [  col1 last: col1 size - col2 size ]
                ifFalse: [ col2 last: col2 size - col1 size ] ).
result.

or

| col result collectionTwo |
result := OrderedCollection new.
collectionTwo := #($a $b $c).
#(1 2 3 4 5) doWithIndex: [: elem : idx |
        result add: elem.
        idx > collectionTwo size
                ifFalse: [ result add: ( collectionTwo at: idx ) ] ].
result

but as Bert said, for the sake of clarity and efficency you should try
to use Streams sending #readStream or #writeStream to the collections.

Hernán

2010/5/4 Bert Freudenberg <[hidden email]>:

> 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 -
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners