Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

string concatenation
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
05-06-05 08:56 AM


Re: string concatenation
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

Report this thread to moderator Post Follow-up to this message
Old Post
Michael Heiming
05-06-05 01:55 PM


Re: string concatenation

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.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
05-06-05 08:55 PM


Re: string concatenation
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

Report this thread to moderator Post Follow-up to this message
Old Post
Janis Papanagnou
05-06-05 08:55 PM


Re: string concatenation
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


Report this thread to moderator Post Follow-up to this message
Old Post
Andrew Schorr
05-06-05 08:56 PM


Re: string concatenation

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.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
05-06-05 08:56 PM


Re: string concatenation
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
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

Report this thread to moderator Post Follow-up to this message
Old Post
Brian Inglis
05-06-05 08:56 PM


Re: string concatenation
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
> wend!

descriptiveName  = descriptiveName period
descriptiveName += period

That's what brevity is supposed to mean. :-)

Janis

Report this thread to moderator Post Follow-up to this message
Old Post
Janis Papanagnou
05-07-05 01:55 AM


Re: string concatenation

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 wend!

How can I when there's characters just wasting away.... ;-) ? Have a
good one yourself.

Ed.








Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
05-07-05 08:55 AM


Re: string concatenation

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.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
05-11-05 01:56 AM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 09:52 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.