Home > Archive > Cobol > April 2007 > Concat array elements into a string
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 |
Concat array elements into a string
|
|
| charles.leviton@gmail.com 2007-04-19, 9:55 pm |
| Hi,
This seems straightforward but I can't seem to figure it out.
Basically, I have a table of elements and I want to string them all
together into a single text variable.
Here is the pseudocode
txtvar PIC X(30)
arrayvar [10]
do varying index from 1 by 1
txtvar = Rtrim(txtvar) || arrayvar(index) where || represents a
concatenation operator
until index = 10
I tried something like this in COBOL
perform varying ws-counter from 1 by 1 until ws-counter = 10
string txtvar ',' arrayvar(ws-counter)
delimited by size into txtvar
end-perform
But I just got spaces in txtvar at the end of it, presumably because I
don't have a trim function that I can use in COBOL (or is there one
that I just don't know of?)
I searched this forum and the web too on things like
"string array elements together" and didn't find anything useful.
Any ideas?
| |
| donald tees 2007-04-19, 9:55 pm |
| charles.leviton@gmail.com wrote:
> Hi,
> This seems straightforward but I can't seem to figure it out.
> Basically, I have a table of elements and I want to string them all
> together into a single text variable.
>
> Here is the pseudocode
>
> txtvar PIC X(30)
> arrayvar [10]
> do varying index from 1 by 1
> txtvar = Rtrim(txtvar) || arrayvar(index) where || represents a
> concatenation operator
> until index = 10
>
> I tried something like this in COBOL
>
> perform varying ws-counter from 1 by 1 until ws-counter = 10
> string txtvar ',' arrayvar(ws-counter)
> delimited by size into txtvar
> end-perform
>
> But I just got spaces in txtvar at the end of it, presumably because I
> don't have a trim function that I can use in COBOL (or is there one
> that I just don't know of?)
>
> I searched this forum and the web too on things like
> "string array elements together" and didn't find anything useful.
>
> Any ideas?
>
move space to txtvar.
> perform varying ws-counter from 1 by 1 until ws-counter = 10
> string txtvar delimited by space (or size)
',' delimited by size
arrayvar(ws-counter) delimited by size
> into txtvar
> end-perform
Donald
| |
| Richard 2007-04-19, 9:55 pm |
| On Apr 20, 6:53 am, charles.levi...@gmail.com wrote:
> Hi,
> This seems straightforward but I can't seem to figure it out.
> Basically, I have a table of elements and I want to string them all
> together into a single text variable.
>
> Here is the pseudocode
>
> txtvar PIC X(30)
> arrayvar [10]
> do varying index from 1 by 1
> txtvar = Rtrim(txtvar) || arrayvar(index) where || represents a
> concatenation operator
> until index = 10
>
> I tried something like this in COBOL
>
> perform varying ws-counter from 1 by 1 until ws-counter = 10
> string txtvar ',' arrayvar(ws-counter)
> delimited by size into txtvar
> end-perform
>
> But I just got spaces in txtvar at the end of it, presumably because I
> don't have a trim function that I can use in COBOL (or is there one
> that I just don't know of?)
>
> I searched this forum and the web too on things like
> "string array elements together" and didn't find anything useful.
When you use 'delimited by size' then you are telling it to use the
size of the variable, ie 30 characters or whatever arrayvar is. You
need to use a delimiting character or perhaps " " (two spaces. You
also need to look at the 'with pointer' clause which specifies where
the string is to start.
You need to space fill the output first. Set the pointer to 1.
I upper cased the added parts. Note I removed txtvar from the string
input list. Txtpoint will be updated by each string to give
concatenation.
MOVE SPACES TO TXTVAR
MOVE 1 TO TXTPOINT
perform varying ws-counter from 1 by 1 until ws-counter = 10
IF ( WS-COUNTER > 1 )
STRING "," DELIMITED BY SIZE
INTO TXTVAR WITH POINTER TXTPOINT
END-IF
string arrayvar(ws-counter) delimited by " "
into txtvar WITH POINTER TXTPOINT
end-perform
| |
| charles.leviton@gmail.com 2007-04-19, 9:55 pm |
| On Apr 19, 4:06 pm, Richard <rip...@Azonic.co.nz> wrote:
> On Apr 20, 6:53 am, charles.levi...@gmail.com wrote:
>
>
>
> When you use 'delimited by size' then you are telling it to use the
> size of the variable, ie 30 characters or whatever arrayvar is. You
> need to use a delimiting character or perhaps " " (two spaces. You
> also need to look at the 'with pointer' clause which specifies where
> the string is to start.
>
> You need to space fill the output first. Set the pointer to 1.
> I upper cased the added parts. Note I removed txtvar from the string
> input list. Txtpoint will be updated by each string to give
> concatenation.
>
> MOVE SPACES TO TXTVAR
> MOVE 1 TO TXTPOINT
> perform varying ws-counter from 1 by 1 until ws-counter = 10
> IF ( WS-COUNTER > 1 )
> STRING "," DELIMITED BY SIZE
> INTO TXTVAR WITH POINTER TXTPOINT
> END-IF
> string arrayvar(ws-counter) delimited by " "
> into txtvar WITH POINTER TXTPOINT
> end-perform
Thank you both. Especially for the addnl logic to avoid the leading
comma.
|
|
|
|
|