Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving a fixed length field to a variable length field 2

Status
Not open for further replies.

hugheskbh

Programmer
Dec 18, 2002
37
US
Hi,

I need to move a 200 byte fixed length field to a variablel length field in Cobol. Can someone help me with an example?
Also, how would I know the length of the variable length field. In other words, the last character of the fixed length field needs to be the length of the variable field.
If anyone can help me, please do so.

Thanks
 
hugheskbh -

Your question is a little confusing, but ...

I assume you are given a 200 byte field and a length byte:
Code:
01.
    05  WS-FIXED-FIELD             PIC X(200).
    05  WS-LENGTH                  PIC X.

COBOL doesn't have variable length fields in the sense most languages might think of them. You can, however, use reference modification to access fields that vary in size at runtime, e.g.:
Code:
MOVE WS-FIXED-FIELD(starting-position:length) TO ...
You don't say what operating system, compiler, and compiler version you are using. That affects how we deal with the WS-LENGTH byte above. Some compilers will allow you to define a single-byte binary field; others won't. Depending on your architecture, there are approaches that you can take to get the length in a workable format. That, as they say, is an exercise left to the reader.

Regards,

Glenn
 
I'm sorry but the file is a one field file coming from a unix operating system. We take the file and upload it to the mainframe. The field is 200 characters fixed length. The problem is if data in the field does not use all positions, spaces are left after the last data character. I need to move the data to a variable length field. For example, if the field is 10 positions and the data in the field is "ken", I need to move "ken" to a 3 position field, but the data can be anything in the field so I have no idea of the length of the data.

Thanks
 
Ummm, Ken, remember this thread: thread209-1206197 ? In that thread this community was never able to determine if you arrived at a correct solution. That thread ends with an unanswered question.

Do you remember this thread: thread209-906362 ? That thread too ends with an unanswered question.

Do you remember this thread, where you asked a question and never returned to the thread: thread209-705710 ? Same here: thread209-431402.

Perhaps this time you will be more explicit with your problem description. A thorough problem description will lead to a direct, usable answer, almost certainly in tess than 18 replies. And the courtesy of a reply helps 'seal the deal' allowing a thread to come to a conclusion.

What and where is "the field" you talk about? You say, "We take the file and upload it. The field is 200 characters fixed length." I have absolutely no idea about where this field is defined. Perhaps if you would show both the UNIX and mainframe sides, we (I) might get it.

Glenn has provided an answer that might be spot on, or it might be wildly wrong, because we don't know anything about your problem yet.

Tom Morrison
 
When I did this, I moved the 200 character fixed record into a table and did a varying from 200 by -1 until I got the first byte with data in it and then moved the data into a work table. It worked good and accomplished what I needed.

Cathy
 
Yah, but the destination of the data, with the so-called 'variable field' still is some unknown object.
Please explain.

TIA
TonHu
 
If I was varying ws-ndx from 200 by -1, when I got to a byte > spaces, I set a ws-num to ws-ndx so I could move the data varying ws-ndx from 1 to by 1 until ws-ndx > ws-num into another ws table. So if ws-num had 3 in it,.e. "ken", I would only move the data from ws-ndx 3 times and the new table would contain "ken" in the first three bytes. I could then move the data to where I needed it.
 
MzKITTY,

I think you are misrepresenting your problem. Your solution is an old solution to strip the field with blanks either left or right. You don't even need to put your field in a table just do a test within the field itself using a PERFORM VARYING.

The problem really is when you write the value of the field to an output field(like a db file). No matter what you do the fixed output field will still have spaces if your input characters is less than the output size. Now if you are talking about CSV file then that's a different story.

Here's an example to strip the field.

MOVE ZEROES TO A. << PIC 9(03) VALUE ZEROS
MOVE ZEROES TO B. << PIC 9(03) VALUE ZEROS


PERFORM
VARYING A FROM 200 BY -1
UNTIL (A = 0)
OR (A <= B)
IF (INPUT-FIELD (A) NOT = SPACES)
MOVE A TO B
END-IF
END-PERFORM.


THEN JUST DO MOVE INPUT-FIELD(1:B) TO SOMETHING.

 
Sorry MzKITTY, it should have been addressed to hugheskbh.

My apology..
 
It's not a csv file, it's a fixed lenght input file. All I'm trying to do is take this 200 character field and based on the data in the field, move it to a field the same size of the actual data in the field. Basically I need the output field to not have spaces.
 
In COBOL you read one file and then output a file, or whatever.

You described the input file as a fixed length file with one fixed length field from a LINUX/Unix OS.

What kind of file are you writing or editing as output?

How are you writing it?

Are you using SQL?

The receiving file needs to be described.

What opereating system is the receiving file on?
What is the File extension or type of file that is receiving the field?

What version of COBOL?

You may want to use SQL or call an external process like some kind of code written in C.



If you do not like my post feel free to point out your opinion or my errors.
 
hugheskbh,

you have been less than clear on what you need, both on this thread and on the others mentioned.

You say you are reading a file with variable size records.
And that you then need to move that field into a "output field".

What is the output field you mention? is it a working storage field? is it a FD (file) field? is it a linkage field?
Or are you moving it into a SQL statement for insertion on a database?

Normally (but not a requirement) if you declara a field as
01 f1 pic x(400).
the field will most times be initialized to spaces by the compiler (but not necessaraly!!!).

So if you move any other field into it with a MOVE statement the resulting field WILL have spaces from the last character of the originating field (considering that a space is not a character) to the end of the 400 chars the above example has.

IF you wish that the spaces to the right are substituted by something else then you will need to make sure of that AFTER you do the move, or optionaly before the move by using a combination of initializing the destination field to your desired value (for example X"00") and then using the STRING verb to move the original field to the destination field (or alternatively using reference modification).

So if you wish to have any further help please supply samples of your imput data, variables used (e.g. names and PICTURE), and code variations you have used so far.




Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
COBOL can only write fixed length fields according to the COBOL Standard, but some Compilers may have built in solutions to write other kinds of fields as an Add-on enhancement to the Compiler. That is why you need to let us know what OS the file will reside on where you are writing the field and what Compiler your are using.

Maybe COBOL is not the best tool in the shed to get this done.

If you do not like my post feel free to point out your opinion or my errors.
 
Do anyone know how to do this? The file is uploaded to the mainframe. I'm using cobol on the mainframe to write to an output file. The file is going to be a flat file just like the input. The only difference is I want the file to be variable length instead of fixed lenght to eliminate spaces at the end of the data.
 
Now you are talking! So it is a flat file then!

You will still have cobol problem regarding your output
structure. I have this same problem before(in mainframe).
The only way we solved our problem is to buy SQR server version for the mainframe. This sqr can create file with varying sizes and is full SQL language tool. This SQR language
product can also be installed in other platforms like AS400,
Tandem etc. But then again that's another story.
 
being a mainframe you can define the file within the JCL on the DD definition with keywork RECFM=VB probably, and then your COBOL file definition must follow it.

As I am not a mainframe guy (just started using it), I cannot be of further help, but I would advise you to search the JCL progrmas existing on your shop for the above string (RECFM), and see if you get any variable one.

Then look at programs that use that file/dataset.

How you do this will also be affected by the COBOL version you use, and the file type you need to create (e.g. QSAM/VSAM/NON VSAM).

You can also go to google, and search the following newsgroup bit.listserv.ibm-main for variable file, and for RECFM and you will find plenty of threads. some will help others wont, but you will learn something with it for sure.

Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
hugheskbh,

There are much to learn ond dealing with file formatting. I have mentioned SQR before but this is only one of several methods of attaining your goal.

If you know any language like C++, perl or java then you can create an API and simply call these APIs from cobol and just supply the parameters. There are a lot of APIs out there but I just don't know if these are free or not.

The question is how do you structure your output? If it is a flat file, still you have a fixed size of your fields. Only csv fields are dynamic but they are delimited and therefore you can always unstring them. With flat file how do you know the FROM and TO position of your fields if they are variable?
 
Hi,

I can use JCL to define the file, but I'm asking is are are there any examples on moving the data from the fixed field to a variable field. I think I have to maybe use an array to count backwards until I find the first character that's not a space, but I'm stuck as to whatelse I need to do.

Thanks
 
hugheskbh,

First, brush up your glasses, and reread this entire thread from the start.
.... (I'm waiting about 5 minutes, no problem.)
.... (Ah, he doesn't come back soon, he switched to his Cobol keyboard and typed a few lines of code, like MzKitty suggested)
Code:
move function length (input-field)   to ws-len
perform until input-field(ws-len:1) <> " " or ws-len = 0
  subtract 1 from ws-len
end-perform
move input-field(1:ws-len)   to output-field
write output-record  *> this should be a line sequential file
.... (Ah, there he is, shouting EUREKA!)

The difficult part of his thread was, as usual, the communication part. Ppl don't act as they are told, but act they think they should act even though they are told differently [jester]

HTH
TonHu
 
Thanks for all of your suggestions. TonHu I'm going to use your example and I would let everyone know the results.

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top