For Programmers: Free Programming Magazines  


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-10, 8:56 pm

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-10, 8:56 pm

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
Janis Papanagnou

2005-05-10, 8:56 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
Ed Morton

2005-05-10, 8:56 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.
Andrew Schorr

2005-05-10, 8: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

Michael Heiming

2005-05-13, 3:58 pm

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
Sponsored Links







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

Copyright 2008 codecomments.com