[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Perl-like strings...?
- From: "Andy Stark" <AStark@...>
- Date: Mon, 09 Oct 2006 10:40:19 +0100
Lua list <lua@bazar2.conectiva.com.br> on Monday, October 9, 2006 at 10:02
+0000 wrote:
>Perl has a STRONG ease in the output of things, by
>allowing variables to be included in the string itself.
>This not only is shorter, but most of all much more
>readable than a lot of ..'s. Could be introduced in Lua as
>syntax sugar, using """ and [[[ operations.
......
>
>print """something $like $this"""
>-->
>print( "something "..like.." "..this )
Instead of this, I would recommend using one of the operators currently
undefined for strings (it could be the % operator, say) and use it to
interpolate values from a table. This is done by detecting placeholders in
the string. For example:-
"Name: [1], Rank: [2], Serial: [3]" % {"Andy", "Captain", 1234}
Of course it doesn't have to use square brackets. One possibility is to
use % within the string like printf does - familiar and consistent with
the choice of operator.
I think this is better because it doesn't require anything major to be
added to the language and is also good from a maintenance point of view.
If you want to change the ordering of the name, rank and serial number,
you just change the ordering of the placeholders; you don't need to alter
any of the code. This is good for internationalisation if nothing else.
A further refinement would be to allow non-numeric indices in the template
and the table, so you could write:-
"Name [fname], Rank: [rankstr], Serial: [sernum]" % {fname="Andy",
rankstr="Captain", sernum=1234}
This would be more self-documenting when the format string and the
interpolation code are kept in separate files, but it obviously adds
complexity to the implementation.
&.
#####################################################################################
This e-mail message has been scanned for Viruses and Content and cleared
by MailMarshal.
The Blackpool Sixth Form College.
#####################################################################################