HP3000-L Archives

June 2000, Week 1

HP3000-L@RAVEN.UTC.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
"COLE,GLENN (Non-HP-SantaClara,ex2)" <[log in to unmask]>
Reply To:
COLE,GLENN (Non-HP-SantaClara,ex2)
Date:
Tue, 6 Jun 2000 17:29:47 -0700
Content-Type:
text/plain
Parts/Attachments:
text/plain (75 lines)
Ernie Newton writes:

> I have a need to pluck out the four rightmost characters of a variable
> length string.  The problem being that the string is alphanumeric and
COBOL
> left-justifies those type of things.  Somewhere in the deep recesses of my
> aged mind, I seem to recall that there is a JUSTIFY command available in
> COBOL.  I could do an algorithm to count the characters, but life would be
> so much simpler if I could right-justify it.

So sorry.  I suspect the "JUSTIFY" you're thinking of is not a verb,
but rather an attribute on a data item, like

   05  FIELD-TITLE   PIC X(20)  JUSTIFY RIGHT.

Unfortunately, this isn't as useful as one would hope.
That is, if you say

   MOVE "ABC   " TO FIELD-TITLE

then the SIX-character string "ABC   " with be right-justified in
FIELD-TITLE, resulting in

            1         2
   ....+....0....+....0
                 ABC

If your string does no contain embedded spaces, then you can get away
with INSPECT...TALLYING...BEFORE " ".

No doubt there are multiple ways to solve this problem, but one with
a (relatively) small amount of code involves first reversing the string
so that those trailing spaces become leading spaces.  Then, we can use
INSPECT...TALLYING...FOR LEADING " " to count the spaces (it's a shame
there's no FOR TRAILING option), and subtract THAT from the field length.

Personally, I determine the field length in the beginning of the
program (using FUNCTION LENGTH rather than a hard-coded var that
I'll forget to change when I change the field), and use that in
subsequent calculations.

Since this technique involves reversing the string -- and thus storing
this result somewhere -- I also determine the length of the hold area,
and require that it be at least as long as the original field.

Without getting into all the field declarations, the process becomes

   init
   ----
      compute field-title-len = function length( field-title )

      compute field-hold-len  = function length( field-hold )

      if field-hold-len < field-title-len
         display "**field-hold needs to be at least as large"
                 " as field-title"
         call "abort"
      end-if


   find-real-length
   ----------------
      move function reverse( field-title ) to field-hold

      move zero to trailing-space-len
      inspect     field-hold
         tallying trailing-space-len
         for leading " "

      compute real-len = field-hold-len - trailing-space-len

Of course, none of the above has been tested. :)

--Glenn

ATOM RSS1 RSS2