For Programmers: Free Programming Magazines  


Home > Archive > Smalltalk > November 2005 > text substitution in strings









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author text substitution in strings
ljw1001@gmail.com

2005-11-27, 7:01 pm

Is there any way to do string substitutions in smalltalk in a way
similar to how they're done in Ruby? Here's a ruby example:

"good morning, #{@name}. How are you today?"

Anything within the #{} is executed and the results inserted in-line.

Thanks.

Ian Bartholomew

2005-11-27, 7:01 pm

ljw1001@gmail.com wrote:
> Is there any way to do string substitutions in smalltalk in a way
> similar to how they're done in Ruby? Here's a ruby example:
>
> "good morning, #{@name}. How are you today?"
>
> Anything within the #{} is executed and the results inserted in-line.


String concatenation.

'good morning, ' , self name , '. How are you today'

That can get a bit slow, not to mention untidy, for multiple arguments
so you could use a Stream.

(String writeStream)
nextPutAll: 'good morning, ';
nextPutAll: self name;
nextPutAll: '. How are you today?';
contents.

Most Smalltalk implementations have also got alternate ways of doing it.
In Dolphin, for example, you could write

'good morning, %s. How are you today' sprintfWith: self name

You could also use #formatWith: or #expandMacrosWith:

--
Ian

Use the Reply-To address to contact me.
Mail sent to the From address is ignored.
Boris Popov

2005-11-27, 7:01 pm

In VisualWorks,

'Hello, <1s>' expandMacrosWith: 'eoJ' reverse

See "StringParameterSubstitution comment" for more details.

-Boris

Bob Nemec

2005-11-28, 7:58 am

In VA you get
#bindWith:
#bindWith:with: (up to 4)
#bindWithArguments: anArray

'good morning, %1. How are you today' bindWith: self name.
'good morning, %1. How are you today, said %2' bindWith: self name with:
self friend.

<...>
> Most Smalltalk implementations have also got alternate ways of doing it.
> In Dolphin, for example, you could write
>
> 'good morning, %s. How are you today' sprintfWith: self name



ljw1001@gmail.com

2005-11-29, 7:03 pm

Thanks for your help.
One or more of these is probably ok for the generation i need to do.

What I also need is something like a "smalltalk server page"
implementation that takes a text stream from a file and performs
replacement in line whenever it hits <%blah%> (or whatever).

I'm guessing something like this has been developed for one or more
smalltalk web servers, though i don't need to serve actual pages, just
do the replacement and output to a file stream. This is perhaps a
better restatement of the original requirement as well since I could
presumably use a string as the stream source as well as a file.

I'd prefer something simple and (ideally) something that works with
squeak. Any thoughts?

Ian Bartholomew

2005-11-29, 7:03 pm

ljw1001@gmail.com wrote:

> I'd prefer something simple and (ideally) something that works with
> squeak. Any thoughts?


Don't know about Squeak but in Dolphin you can use
#copyReplaceAll:with:, as in

s := 'Replace each of <%blah%> occurrences of <%blah%> embedded marker
with the string "<%blah%>"'.
s copyReplaceAll: '<%blah%>' with: 'the'

Have a look through the Squeak String class and it's superclasses, there
should be something similar.

--
Ian

Use the Reply-To address to contact me.
Mail sent to the From address is ignored.
David Given

2005-11-29, 7:03 pm

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

In article <1133276293.165316.312470@z14g2000cwz.googlegroups.com>,
ljw1001@gmail.com writes:
[...]
> What I also need is something like a "smalltalk server page"
> implementation that takes a text stream from a file and performs
> replacement in line whenever it hits <%blah%> (or whatever).


I haven't done this in Smalltalk, but I've done it in a bunch of different
languages.

Lua has a routine that takes a regexp and for every matching substring,
converts it to a replacement provided by a callback. If this functionality
is available, you could do (pseudocode follows):

string := string forRegexp: '<%(.*)%>' do: [ :capture |
^ keys at: capture
].

Another technique is to use a routine that divides a string up into words;
give it your delimiter, and then every even numbered 'word' will be inside
the delimiters, and every odd numbered 'word' will be outside. This
requires your delimiters to be the same both ends, though:

list := string split: '%'.
list do: [ :i |
((i % 2) == 0) ifTrue: [ | j |
j := list at: i.
list at: i put: (keys at: j)
]
].
(you'd then need to put the string back together again, which is dead easy,
but I can't remember the name of the method that does it.)

- --
+- David Given --McQ-+ "Why should we put ourselves out of our way to
| dg@cowlark.com | serve posterity? For what has posterity ever done
| (dg@tao-group.com) | for us?" --- Sir Boyle Roche
+- www.cowlark.com --+
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFDjJPXf9E0noFvlzgRAvxXAJwLImpRqlhe
lwkZpc9+o1xET9OWBACgpMPD
pjpGzzWpY2OzXsSkIcXBg8k=
=LbV7
-----END PGP SIGNATURE-----
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com