Concatenating strings stored in an ordered collection

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

Concatenating strings stored in an ordered collection

Paul DeBruicker
Hi

I've created an ordered collection of strings.  I'd like to paste each
element of the ordered collection together into one long string that
contains all of the elements of the ordered collection.

So given this:
| col |
col := OrderedCollection with: 'asdf' with: '1234' with: 'aaaa'.


I'd like to end up with this:

'asdf1234aaaa'

I've come up with this:
| col |
col := OrderedCollection with: 'asdf' with: '1234' with: 'aaaa'.
String streamContents: [:stream | col do: [:i | stream print: i]]

but that gives:
'''asdf''''1234''''aaaa'''

What else should I do?  Thanks

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

Re: Concatenating strings stored in an ordered collection

Rob Rothwell
Try output := col inject: '' into: [:a :b | a,b].

And check out the terse guide to Squeak:

http://wiki.squeak.org/squeak/5699.

I can never remember the format for inject, but got it right somehow this time!

Rob

On Fri, May 22, 2009 at 3:51 PM, Paul DeBruicker <[hidden email]> wrote:
Hi

I've created an ordered collection of strings.  I'd like to paste each
element of the ordered collection together into one long string that
contains all of the elements of the ordered collection.

So given this:
| col |
col := OrderedCollection with: 'asdf' with: '1234' with: 'aaaa'.


I'd like to end up with this:

'asdf1234aaaa'

I've come up with this:
| col |
col := OrderedCollection with: 'asdf' with: '1234' with: 'aaaa'.
String streamContents: [:stream | col do: [:i | stream print: i]]

but that gives:
'''asdf''''1234''''aaaa'''

What else should I do?  Thanks

Paul
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


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

Re: Concatenating strings stored in an ordered collection

Bert Freudenberg
In reply to this post by Paul DeBruicker

On 22.05.2009, at 12:51, Paul DeBruicker wrote:

> Hi
>
> I've created an ordered collection of strings.  I'd like to paste each
> element of the ordered collection together into one long string that
> contains all of the elements of the ordered collection.
>
> So given this:
> | col |
> col := OrderedCollection with: 'asdf' with: '1234' with: 'aaaa'.
>
>
> I'd like to end up with this:
>
> 'asdf1234aaaa'
>
> I've come up with this:
> | col |
> col := OrderedCollection with: 'asdf' with: '1234' with: 'aaaa'.
> String streamContents: [:stream | col do: [:i | stream print: i]]
>
> but that gives:
> '''asdf''''1234''''aaaa'''
>
> What else should I do?  Thanks


Use nextPutAll: instead of print:.

- Bert -


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

Re: Concatenating strings stored in an ordered collection

onierstrasz

You could use the SplitJoin package.

http://www.squeaksource.com/SplitJoin.html

'' join: (OrderedCollection with: 'asdf' with: '1234' with: 'aaaa') --
 > 'asdf1234aaaa'

Cheers,
- on

On May 22, 2009, at 23:14, Bert Freudenberg wrote:

>
> On 22.05.2009, at 12:51, Paul DeBruicker wrote:
>
>> Hi
>>
>> I've created an ordered collection of strings.  I'd like to paste  
>> each
>> element of the ordered collection together into one long string that
>> contains all of the elements of the ordered collection.
>>
>> So given this:
>> | col |
>> col := OrderedCollection with: 'asdf' with: '1234' with: 'aaaa'.
>>
>>
>> I'd like to end up with this:
>>
>> 'asdf1234aaaa'
>>
>> I've come up with this:
>> | col |
>> col := OrderedCollection with: 'asdf' with: '1234' with: 'aaaa'.
>> String streamContents: [:stream | col do: [:i | stream print: i]]
>>
>> but that gives:
>> '''asdf''''1234''''aaaa'''
>>
>> What else should I do?  Thanks
>
>
> Use nextPutAll: instead of print:.
>
> - 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