Re: Right justifying (arbitrary) alphanumeric input

From: James J. Gavan (jgavandeletethis_at_shaw.ca)
Date: 03/03/05


Date: Wed, 02 Mar 2005 23:32:43 GMT

epc8@juno.com wrote:
>
> Thanks. I have since learned that expressions are allowed in reference
> modificaion, once I separate the operators from the operands. So the
> program simplifies to this:
>
> identification division.
> program-id. test1.
> data division.
> working-storage section.
> 01 x pic x(26) value "abcdefghijklmnopqrstuvwxyz".
> 01 y pic x(26).
> 01 i pic 99.
> 01 j pic 99.
> procedure division.
> main.
> compute j = function length(x).
> perform next-i varying i from 0 by 1
> until i is greater than j.
> stop run.
> next-i.
> move x to y.
> if i is > 0 and i < j perform
> move function reverse(y(1:i)) to y(1:i)
> move function reverse(y(i + 1:j - i)) to y(i + 1:j - i)
> move function reverse(y) to y
> end-perform.
> display i space y.
>
> -- Elliot
>

OK so it works - but what does it actually do ? (Lazy way - I compiled
and ran it) - but it will hiccup if you do the following. Take your two
fields pic x(26) and make them pic x(44, or 50, or....). Take the
"a.....z" and put the relevant number of spaces in FRONT of the
alphabet, similarly put the spaces after the alphabet - although you
don't need to do that because the spaces are understood when you say pic
x(44). Other permutations using pic x(44), some spaces in front and at
the end, and split the alphabet so there are spaces in the middle -

  "abcdefghij klmnopqrstuvwxyz".

To be worthwhile it has to handle the above options and possibly more I
haven't thought of. I can do it quite simply using some OO methods (The
code is already there without me thinking about it) - but that's not
much help to you.

But I can give you some pointers, and this is NOT a
recommendation/suggestion how you should do it using Procedural COBOL -
the string and length are passed to a method which using an array
technique, (a TABLE in COBOL parlance), i.e. each incoming character
(not just alpha or numeric) is identified by a position/index in the
array. Take the split alpha above - it ignores the spaces at the front
and end and the spaces between "...hij" and "klm". Trying to keep this
simple - it has a second array of 'words' the 'semi-words' you find
without a space - those 'semi-words' get stored with differing lengths
and then they are all STRUNG back together again.

You need to keep a count of the words it finds. So what's happening in
OO - your y pic x(44) is initialized then I can do a

perform varying n from 1 by 1 until n > MyWordCount

    STRING the current "semi-word" into y.
    Second time around y contains the first 'semi-word' and now
       we string the second 'semi-word" into the existing y
    etc...

end-perform

Simplest way I would suggest, keeping a count of total characters found
- do a Reference Modification where you want right justified, otherwise
automatically left justified.

Now the original version I wrote, using the support methods just handled
strings with the option of including one space between 'real words' in
the resultant 01 y pic x(44) above, or string the whole thing together
ignoring ALL spaces. Then because I want to take a text field and
display it in a Dialog or as a label in a Treeview, then I added options
to get back the text with/without x'00' at the end.

If the last bit sounds like first-class gibberish :-

01 x pic x(50) value "George Washington".

then you get back

01 y pic x(50)

which will have a value of 'George Washington" + x'00' - a 'real' total
of 18 used characters.

Jimmy, Calgary AB



Relevant Pages

  • Re: good algorithms come with practice and reading good code/books?
    ... All I really use that book for is a reference to the c library functions. ... if given an input stream's file pointer, whereas in fact the behaviour is ... the source string is longer than or equal to count characters. ... thus ensuring that no array boundary overflow occurs. ...
    (comp.lang.c)
  • Re: algorithm to compare 3 array elements
    ... but the back reference doesn't. ... So if you make the regex global (adding ... return string; ... characters, and not skipping the first two. ...
    (comp.lang.javascript)
  • [AustrE or NeoZE?] peculiar pronoun usage
    ... Shirley Hazzard in her novel /The Great Fire/ has some of her characters use the pronoun "she" in a peculiar way. ... For instance, this, in reference to a letter: ... a father has been promising his wife and daughter that they would make a trip to Britain, but has repeatedly broken his promise over the years. ...
    (alt.usage.english)
  • Re: PIC P put to real use
    ... I think this is a little more obvious than the reference ... it is a programming language. ... characters being replaced as well as the replacement text! ... I actually like that better than the reference modification. ...
    (comp.lang.cobol)