Code Comments
Programming Forum and web based access to our favorite programming groups.I often find myself writing scripts like this to continudally add to the
end of a string, then print the final result:
{text = text $0}END{print text}
If I had a similar problem involving adding numbers, I would write it as:
{num += $0}END{print num}
i.e. use the "+=" operator to indicate that $0 should be added to the
variable on the left side of the operator rather than having to specify
the variable twice.
I'd like to be able to do something similar for strings, e.g.:
{text += $0}END{print text}
but this won't work since the "+=" will convert the string to a number.
Is there an equivalent string operator to "+=" that I'm just not seeing
in the reference material (e.g. "&=" or ".=" or "#=" or ....)?
If not, anyone got any cute ways of doing that? Best I can come up with
is define a function:
function app(t,n) { t = t n }
{app(text,$0)}END{print text}
or to just use sub(), e.g.:
{sub("$",$0,text)}END{print text}
Regards,
Ed.
Post Follow-up to this messageIn comp.lang.awk Ed Morton <morton@lsupcaemnt.com>:
> I often find myself writing scripts like this to continudally add to the
> end of a string, then print the final result:
> {text = text $0}END{print text}
Unsure if I understood the problem?
awk 'ORS=" "' infile
Might be an idea.
[..]
--
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)
mail: echo zvpunry@urvzvat.qr | perl -pe 'y/a-z/n-za-m/'
#bofh excuse 173: Recursive traversal of loopback mount points
Post Follow-up to this messageMichael Heiming wrote: > In comp.lang.awk Ed Morton <morton@lsupcaemnt.com>: > > > > > > Unsure if I understood the problem? > > awk 'ORS=" "' infile > > Might be an idea. That would work (with ORS="") for the specific example I gave, but I'm talking about the general question of adding some new string to the end of an existing one, e.g. given 2 strings x and y I'm looking for a way to be able to abbreviate this: x = x y to this: x += y or similar. Thanks, Ed.
Post Follow-up to this messageEd Morton wrote: > > [...] but I'm > talking about the general question of adding some new string to the end > of an existing one, e.g. given 2 strings x and y I'm looking for a way > to be able to abbreviate this: > > x = x y > > to this: > > x += y > > or similar. The problem is that there's no _explicit_ concatenation operator. And the op= is defined for existing operators op. So I don't think it's possible the way you would like it, you have to ressort to x = x y or a function. Janis
Post Follow-up to this messageIn case it matters, gawk (at least as of version 3.1.4) optimizes the expression 'x = x y', so I don't think there would be any gain in performance from expressing it differently. Regards, Andy
Post Follow-up to this messageAndrew Schorr wrote: > In case it matters, gawk (at least as of version 3.1.4) optimizes the > expression 'x = x y', so I don't think there would be any gain in > performance from expressing it differently. I'm just looking for syntactic brevity. Thanks, Ed.
Post Follow-up to this messageOn Fri, 06 May 2005 09:31:04 -0500 in comp.lang.awk, Ed Morton <morton@lsupcaemnt.com> wrote: > > >Andrew Schorr wrote: > >I'm just looking for syntactic brevity. x=x y has one more space than x+=y: you're obsessing; enjoy the wend! -- Thanks. Take care, Brian Inglis Calgary, Alberta, Canada Brian.Inglis@CSi.com (Brian[dot]Inglis{at}SystematicSW[dot]a b[dot]ca) fake address use address above to reply
Post Follow-up to this messageBrian Inglis wrote: > On Fri, 06 May 2005 09:31:04 -0500 in comp.lang.awk, Ed Morton > <morton@lsupcaemnt.com> wrote: > > x=x y has one more space than x+=y: you're obsessing; enjoy the > wend! descriptiveName = descriptiveName period descriptiveName += period That's what brevity is supposed to mean. :-) Janis
Post Follow-up to this messageBrian Inglis wrote: > On Fri, 06 May 2005 09:31:04 -0500 in comp.lang.awk, Ed Morton > <morton@lsupcaemnt.com> wrote: > > > > > x=x y has one more space than x+=y: Yes, but: x += y saves 33% of characters compared to: x = x + y ;-) > you're obsessing; I'm a software engineer. It's what we do. > enjoy the wend! How can I when there's characters just wasting away.... ;-) ? Have a good one yourself. Ed.
Post Follow-up to this messageAndrew Schorr wrote: > In case it matters, gawk (at least as of version 3.1.4) optimizes the > expression 'x = x y', so I don't think there would be any gain in > performance from expressing it differently. I'm just looking for syntactic brevity. Thanks, Ed.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.