[Question]Yet another question. (Collections)

John Clonts jclonts at mastnet.net
Thu May 11 21:45:35 UTC 2000


Karl Ramberg wrote:
> 
> Thanks for the answer. That helps a little but what I want to do is
> when I have a collection of say
> 
>   col := #('1' '2' '3' '4') asOrderedCollection
>     numbers := col collect: [ :each | each asNumber ]
> 
> Then some code so I end up with a integer with value 1234.
> 

Karl Ramberg wrote:
> 
> Thanks for the answer. That helps a little but what I want to do is
> when I have a collection of say
> 
>   col := #('1' '2' '3' '4') asOrderedCollection
>     numbers := col collect: [ :each | each asNumber ]
> 
> Then some code so I end up with a integer with value 1234.
> 

Since the experts have not answered you yet, us novices rise up...

Here's two fragments to look at.

cols := #('1' '2' 34 $5 '67' '8').
strm := WriteStream on: ''.
cols do: [ :o | strm nextPutAll: o asString ].
strm contents asNumber. "inspect --> 12345678"

"then do...."
strm reset.
cols do: [ :o | strm nextPut: o asString first].
strm contents asNumber. "inspect --> 123568"

Though your example had strings as the collection's elements, Character
is probably the best choice (like $5 above).  Otherwise you have to deal
with each element's possibly having multiple characters each, and either
accomodate them (fragment 1) or ignore the additional ones (fragment 2).

I hope this helps you explore towards your "final answer"!

Cheers,
John

P.S. New Emoticons/Smileys(/Smallies?), what do y'all think:

[:o|]     -- Mr. Buster Blockblaster, KOSB [|]

do:[:o|]  -- Buster with infant son on head wearing baseball cap

[:o| self enhance: o]  -- Buster wearing T-Shirt expressing: 
                       "I add value to everything I'm given"





More information about the Squeak-dev mailing list