Home > Archive > AWK > May 2005 > string concatenation
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 |
string concatenation
|
|
| Ed Morton 2005-05-06, 3:56 am |
| 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.
| |
| Michael Heiming 2005-05-06, 8:55 am |
| In 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
| |
| Ed Morton 2005-05-06, 3:55 pm |
|
Michael 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.
| |
| Janis Papanagnou 2005-05-06, 3:55 pm |
| Ed 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
| |
| Andrew Schorr 2005-05-06, 3:56 pm |
| 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.
Regards,
Andy
| |
| Ed Morton 2005-05-06, 3:56 pm |
|
Andrew 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.
| |
| Brian Inglis 2005-05-06, 3:56 pm |
| On 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
w end!
--
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
| |
| Janis Papanagnou 2005-05-06, 8:55 pm |
| Brian 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
> w end!
descriptiveName = descriptiveName period
descriptiveName += period
That's what brevity is supposed to mean. :-)
Janis
| |
| Ed Morton 2005-05-07, 3:55 am |
|
Brian 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 w end!
How can I when there's characters just wasting away.... ;-) ? Have a
good one yourself.
Ed.
| |
| Ed Morton 2005-05-10, 8:56 pm |
|
Andrew 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.
| |
| Janis Papanagnou 2005-05-10, 8:56 pm |
| Brian 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
> w end!
descriptiveName = descriptiveName period
descriptiveName += period
That's what brevity is supposed to mean. :-)
Janis
| |
| Ed Morton 2005-05-10, 8:56 pm |
|
Brian 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 w end!
How can I when there's characters just wasting away.... ;-) ? Have a
good one yourself.
Ed.
| |
| Brian Inglis 2005-05-10, 8:56 pm |
| On 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
w end!
--
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
|
|
|
|
|