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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Difficult recognizable tricks in COBOL 11

Status
Not open for further replies.

Crox

Programmer
Apr 3, 2000
893
NL
I am looking for tricks in COBOL. I need examples of code that is doing something else then you think at first sight.

Thanks in advance!

Regards,

Crox
 
And another one, does not work in all implementations of COBOL

01 varx.
05 vx pic x(10) value "abcd".
05 vy pic x(10) value "efg".
05 varb.
10 varo occurs 5000.
15 vx pic x(10).
15 vy pic x(10).

move varx to varb.

In some implementations it will result the whole group varx being populated with the values of vx and vy.

In others only the first occurs will be populated.

Not sure if this is what you were looking for.



Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
An optimalisation trick.
Code:
01 work.
   03 w-amount         pic S9(7)V9(7).
*> factor 10
   03 w-amount-div0010 redefines
      w-amount         pic S9(6)V9(8).
   03 w-amount-mult010 redefines
      w-amount         pic S9(8)V9(6).
*> factor 100
   03 w-amount-div0100 redefines
      w-amount         pic S9(5)V9(9).
   03 w-amount-mult100 redefines
      w-amount         pic S9(9)V9(5).

   03 w-target-amount1 pic S9(8)V9(8).
   03 w-target-amount2 pic S9(8)V9(8).

Instead of
Code:
compute w-target-amount1 = w-amount / 10.
compute w-target-amount2 = w-amount * 100.
you code
Code:
move w-amount-div0010 to w-target-amount1.
move w-amount-mult100 to w-target-amount2.

So when powers of ten are involved COBOL never has to calculate, just redefine the field and exploid the V in your picture clause.
 
Here's another one:

Code:
01 hey-im-an-array            occurs 1000 times indexed by
   hey-idx.
   03 hey-array-elem.
      05 hey-array-field1        pic x(03).
      05 hey-array-yes-no-switch pic x(01).
      05 hey-array-packed-field  pic S9(7) comp-3.
      05 hey-array-binary-field  pic S9(9) comp-5.
To initailze don't code:
Code:
set hey-idx to 1.
perform 1000 times
   move space to hey-array-field1        (hey-idx)
   move "N"   to hey-array-yes-no-switch (hey-idx)
   move zero  to hey-array-packed-field  (hey-idx)
                 hey-array-binary-field  (hey-idx)
   set hey-idx up by 1
end-perform.
But:
Code:
move space                  to hey-array-field1        (1).
move "N"                    to hey-array-yes-no-switch (1).
move zero                   to hey-array-packed-field  (1).
                               hey-array-binary-field  (1).
move ALL hey-array-elem (1) to hey-im-an-array.
 
Code:
77  Toggle  Pic 9 Value 1.
. . .
    Subtract 1 from Toggle
. . .
    Subtract 1 from Toggle
. . .
    If Toggle = Zero
        etc.
 
webrabbit,

Could you explain that one? I'm still scratching my head.
 
Dimandja,

Notice -- no 'S' on the picture, so the first subtract makes the value zero, the next subtract (assuming you don't use ON SIZE ERROR) makes the value one (!!) and on and on. It is a trick I commonly use when I want to do something on, say, alternating lines or alternating pages.

Tom Morrison
 
It is possible like this:
Code:
01 hey-im-an-array.
 02 occurs 1000 times indexed by
   hey-idx.
   03 hey-array-elem.
      05 hey-array-field1        pic x(03).
      05 hey-array-yes-no-switch pic x(01).
      05 hey-array-packed-field  pic S9(7) comp-3.
      05 hey-array-binary-field  pic S9(9) comp-5.
       
01 REDEFINES hey-im-an-array.
   03  FIRST-HEY                 PIC X(13).
   03  REST-HEY                  PIC X(12987).
 

01  INITIAL-ELEMENT.
      05 HEI-array-field1        pic x(03)   VALUE SPACE.
      05 HEI-array-yes-no-switch pic x(01)   VALUE 'N'.
      05 HEI-array-packed-field  pic S9(7) comp-3 VALUE ZERO.
      05 HEI-array-binary-field  pic S9(9) comp-5 VALUE ZERO.

       
      MOVE INITIAL-ELEMENT TO FIRST-HEY.
      MOVE hey-in-an-array TO REST-HEY.
This is a well known trick. It works very efficient on IBM mainframes but also in CA-REALIA COBOL.
 
Frederico,

I like your cycling trick; have a star!

I also think you got there first with the table initialisation which, as you say, is implementation-dependent.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Tom (k5tm),
You state that the code:
Code:
77  Toggle  Pic 9 Value 1.
. . .
    Subtract 1 from Toggle
. . .
    Subtract 1 from Toggle
. . .
    If Toggle = Zero
        etc.
is a trick you commonly use when you want to do something on, say, alternating lines or alternating pages.

This is a pretty unusual (IMHO) piece of code and is quite confusing. Do you not think that there is a clearer way to do it?
(DIVIDE LINE-COUNT/PAGE-COUNT BY 2 REMAINDER ODD-EVEN
IF ODD-EVEN = 0 etc springs easily to mind)

No criticism implied here, it's just that I like code to be as straight forward as possible and that code made me go 'Derrr...'!!

Marc
 
The first solution is very efficient. The question for this thread is "difficult recognizable". I like it a lot!
 
I don't know if this is a "trick" or not (or just "confusing", i.e. the requested

"doing something else then you think at first sight."

Code:
01  Group-Item.
     05  GI-1   Pic X Value "1"
     05  GI-2   Pic X Value "2".
01  AlphaNum-Edited
                Pic X/X.
  ...

Move Group-Item to AlphaNum-edited.


Bill Klein
 
Bill,

I ran that code and got this:

Code:
GROUP-ITEM =
  GI-1 = "1"
  GI-2 = "2"
ALPHANUM-EDITED = "12 "

Where is the "trick"?
 
The "unexpected results" are that this is NOT the same thing as:

01 Group-Item.
05 GI-1 Pic X Value "1"
05 GI-2 Pic X Value "2".
01 AlphaNum-Edited
Pic X/X.

Move Group-Item (1:) to AlphaNum-Edited

which many programmers think it should be (or at least the first time they "fall into it"), i.e. a GROUP move is *not* the same thing as an alphanumeric elementary move.

Bill Klein
 
All righty, then. My second run did give "1/2".

But, these are rules - not exactly a trick. I have the same problem with the PIC 9 VALUE 1 subtraction "trick".

When rules are ignored, the right way to do things or vice-versa shouldn't be considered a trick - COBOL is simply doing what it was explicitly designed to do. You say so: "a GROUP move is *not* the same thing as an alphanumeric elementary move".

On the other hand, I like the other tricks - although they really fall into the "undocumented" COBOL features: many other languages explicitly support those "tricks".

My theory is that COBOL is so rigidly specified (a la french academy) that there isn't much room for improvisation - the french language suffers similarly too.

Dimandja

 
I was responding to the part of the original post that said,

" I need examples of code that is doing something else then you think at first sight."


Bill Klein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top