Re: [Pharo-project] gsub like solution for Pharo/Smalltalk
Hi, this is an alternative script, i've added a couple of extra examples you might find useful. Evaluate them in a workspace. delimiter := $@. string := 'key1@value1@key2@value2'. substrings := string subStrings: {delimiter}. dictionary := Dictionary new. substrings groupsOf: 2 atATimeDo:[:each| dictionary at: each second put: each first ]. str := '' writeStream. dictionary storeOn: str. str contents. str := '' writeStream. dictionary keysAndValuesDo:[:key :value| str nextPutAll: value ; nextPut: delimiter ; nextPutAll: key ]. str contents Fernando On Tue, Nov 15, 2011 at 4:15 AM, mail list <mail.list@ficonab.com> wrote:
Nicolas, Thanks. Â Verbose is fine. Â I will give your suggestion a try.
I also commented on fernando's email below.
S. On Nov 15, 2011, at 3:26 AM, Nicolas Cellier wrote:
It might seem a bit verbose, but your example translates as
aDictionary associations inject: string into: [:subst :assoc | assoc key asRegex copy: subst replacingMatchesWith: assoc value].
Nicolas
2011/11/14 Fernando Olivero <fernando.olivero@usi.ch>:
I dont fully understand your question, also i have no idea what #gsub does. gsub does a global search and replace in the string to replace any elements with the replacement text. mystring:='hello there hello'
eg myString.gsub('hello','goodbye') Â ===> 'goodbye there goodbye'
S.
do you start from a dictionary ? or a string ? it seems that you want to create a dictionary from a particular string.
Could you provide an example of such string, i could help you in finding the correct messages of String/Stream to send.
The #gsub message, seems pertinent to all Strings? or is a special behavior for your special case?
Fernando
On Mon, Nov 14, 2011 at 10:17 AM, Sven Van Caekenberghe <sven@beta9.be> wrote:
On 14 Nov 2011, at 08:25, mail list wrote:
I have  a dictionary of keys and values.
I have a text string in which the keys are marked with before and ending symbols.
In ruby to replace the key with the value in the text string is reasonably simple. Â (eg Dictionary.each { key,value | string.gsub!(key,value) } Â - or similar to this.
While I don't mind rolling my version of this in Smalltalk, just exploring the regex code and the streams code it is not obvious where is the easiest place to start.
If someone would put their comments on where to start I will build the functionality and put it up on squeaksource since I think it is common requirement.
S.
There is String>>#format: but there are probably other options as well.
Sven
participants (1)
-
Fernando Olivero