COBOL

COBOL

COBOL

COBOL is responsible for the efficient, reliable, secure, and unseen day-to-day operations of the world's economy.


#Programminglanguages
 View Only
  • 1.  data type determined at run time

    Posted 10/20/16 04:30 PM

    Is there a way to determine if a field is packed-decimal,alphanumeric, binary etc at run time?  For example "If field-1 usage is packed-decimal perform my-routine.

    ninjaken


  • 2.  Re: data type determined at run time

    Posted 10/20/16 05:15 PM

    There isn't a way to determine what type a data item is, and that's not something you need to determine at runtime anyway. If field-1 is PIC 9(5)V9(4) PACKED-DECIMAL, it's always packed-decimal.

     

    If you're asking about at the contents, you could do this:

    MOVE FIELD-1 TO FIELD1-PACKED.
    IF FIELD1-PACKED IS NUMERIC PERFORM MY-ROUTINE.

    (You'd of course have to create data item FIELD1-PACKED and have it be the appropriate size, and signed or unsigned as desired).

    COBOL doesn't provide a way to interpret the bits like you wanted, though, because COBOL data is very dependent on the picture clause, and without the picture clause, it's hard to know exactly how the data should be interpreted.

    Mike Chase


  • 3.  Re: data type determined at run time

    Posted 10/20/16 05:31 PM

    Perhaps you could explain why you think it would be useful for you to know? If you don't know when you are entering the code, then that's it, your opportunity is gone.

    The compiler takes care of any necessary/possible conversions for the way you code your statements, and if conversions are not possible (like a PIC A to a PIC 9 or COMP-1/COMP-2) the compiler will happily and accurately point that out to you.

    There is no indication within a compiled program of what any given piece of storage was defined as originally, and "looking at" the data doesn't give you certainty either. X'F0F0' could be a PIC XX or a binary field. X'000C' could be binary or packed-decimal or something that happened to be stored in a PIC XX (all every possible bit-pattern is equally valid).

    There's also REDEFINES. And REDEFINES on REDEFINES.

    With good data-naming, you don't often even need to know exactly how it is defined when you type the code, let alone have to know specifically at run-time for any reason I can think of.

    BillWoodger


  • 4.  Re: data type determined at run time

    Posted 10/26/16 11:15 AM

    I told my people that it was not possible but they just needed to hear it from a higher authority. Thanks for your response

    ninjaken


  • 5.  Re: data type determined at run time

    Posted 10/28/16 12:50 PM

    It might be possible with some COBOL compiler on some platform, but I've never seen it done in 40+ years of COBOL coding. As has been nicely implied, it's not particularly relevant to COBOL. If the capability is needed, then a very different language should be used, both to populate the memory and to access it. (The two must be compatible.) It doesn't fit within the types of tasks that COBOL should be used for.

    With that said, there are a couple methods that might be used on at least some IBM platforms that can help you with 'educated guesses' between some data types. Most often, the guesses tend to be elimination of data types rather than a selection of a specific type.

    That is, you can often look at memory and potentially see values that cannot be packed-decimal, so you could eliminate that data type. That's not particularly useful, though, because it doesn't tell you what is actually there. And there is no way ever to eliminate binary integer values. No matter what data type was assigned to a memory area, the bit pattern can always be interpreted as a binary integer value.

    tomliotta