String and Unstring 
Author Message
 String and Unstring

Hi, could anyone tell me how to deal with following problem:

        01 price     pic x(6).
        01 new-price pic x(5).

and data looks like this: 9.25   19.43   350.53. I want to remove the dot
so that data will look like ths:925   1943  35053. I tried unstring
        UNSTRING PRICE DELIMIETED BY '.'
                INTO DOLLARS
                          CENTS
        STRING DOLLARS DELIMITED BY SIZE
                        CENTS DELIMITED BY SIZE
                INTO NEW-PRICE
where dollars has pic x(3), cents pic x(2). The problem is that characters
in DOLLARS are left justified, therefore 9.25 become 9--25 instead of  
--925 in new-price. Is there a way that you can right justify characters in
an alphanumeric field? Thanks in advance.  John
--



Fri, 12 Nov 1999 03:00:00 GMT  
 String and Unstring

On Mon, 26 May 1997 16:43:41 GMT, "john"

Quote:
>Hi, could anyone tell me how to deal with following problem:

>    01 price     pic x(6).
>    01 new-price pic x(5).

>and data looks like this: 9.25   19.43   350.53. I want to remove the dot
>so that data will look like ths:925   1943  35053. I tried unstring
>    UNSTRING PRICE DELIMIETED BY '.'
>            INTO DOLLARS
>                      CENTS
>    STRING DOLLARS DELIMITED BY SIZE
>                    CENTS DELIMITED BY SIZE
>            INTO NEW-PRICE
>where dollars has pic x(3), cents pic x(2). The problem is that characters
>in DOLLARS are left justified, therefore 9.25 become 9--25 instead of  
>--925 in new-price. Is there a way that you can right justify characters in
>an alphanumeric field? Thanks in advance.  John

There's lots of ways you could do this but the simplest in *this* case
is surely to forget about using STRING and use reference modification
instead:

        MOVE PRICE(1:3) TO NEW-PRICE
        MOVE PRICE(5:2) TO NEW-PRICE(4:)

In the more general case I normally handle pre-formatted numerics
either byte by byte or convert then back to 9(n) using and INSPECT to
replace the " " by "9" but I'm sure people can suggest lots of other
ways.

--
Paul Oldham, Milton, Cambridge, UK
http://www.the-hug.demon.co.uk/paul/
To email me take the "x" off the *reply-to* address



Mon, 15 Nov 1999 03:00:00 GMT  
 String and Unstring



Quote:
> Hi, could anyone tell me how to deal with following problem:

>    01 price     pic x(6).
>    01 new-price pic x(5).

> and data looks like this: 9.25   19.43   350.53. I want to remove the dot
> so that data will look like ths:925   1943  35053. I tried unstring

Here's a quick, and I mean really quick attempt.

Move 5 to new-price-pointer

Perform with test after varying tally from 6 by -1 until tally = zero
  if price (tally:1) not = "."
    move price (tally:1) to new-price (new-price-pointer:1)
    subtract 1 from new-price-pointer
  end-if
end-perform
.

I think that will work!  Tally may or may not be a register in your
compiler, and you need to define new-price-pointer.

I love reference modification!



Mon, 15 Nov 1999 03:00:00 GMT  
 String and Unstring



Quote:



> > Hi, could anyone tell me how to deal with following problem:

> >       01 price     pic x(6).
> >       01 new-price pic x(5).

> > and data looks like this: 9.25   19.43   350.53. I want to remove the
dot
> > so that data will look like ths:925   1943  35053. I tried unstring

> Here's a quick, and I mean really quick attempt.

> Move 5 to new-price-pointer

> Perform with test after varying tally from 6 by -1 until tally = zero
>   if price (tally:1) not = "."
>     move price (tally:1) to new-price (new-price-pointer:1)
>     subtract 1 from new-price-pointer
>   end-if
> end-perform
> .

> I think that will work!  Tally may or may not be a register in your
> compiler, and you need to define new-price-pointer.

> I love reference modification!

I know I'm talking to myself.  Someone else posted a simpler example, and
it works if the decimal is always 2 positions.  Also one small change to
the above could make it work reguardless of the justification of the
incoming number.  Here's a better attempt.

Move 5 to new-price-pointer
Move spaces to new-price
 Perform with test after varying tally from 6 by -1 until tally = zero
   if price (tally:1) numeric
     move price (tally:1) to new-price (new-price-pointer:1)
     subtract 1 from new-price-pointer
   end-if
 end-perform
.

Yes... that should handle the input nicely, regardless of justification and
end up with a right justified space filled field with no decimal points.

The only flaw in this would be in the input number has no decimal and is 6
digits long, in which case the program will move the last number into
new-price (0:1) and results are unpredictable.



Mon, 15 Nov 1999 03:00:00 GMT  
 String and Unstring

Quote:

>Hi, could anyone tell me how to deal with following problem:

>    01 price     pic x(6).
>    01 new-price pic x(5).
>and data looks like this: 9.25   19.43   350.53. I want to remove the dot
>so that data will look like ths:925   1943  35053. I tried unstring
>    UNSTRING PRICE DELIMIETED BY '.'
>            INTO DOLLARS
>                      CENTS
>    STRING DOLLARS DELIMITED BY SIZE
>                    CENTS DELIMITED BY SIZE
>            INTO NEW-PRICE
>where dollars has pic x(3), cents pic x(2). The problem is that characters
>in DOLLARS are left justified, therefore 9.25 become 9--25 instead of  
>--925 in new-price. Is there a way that you can right justify characters in
>an alphanumeric field? Thanks in advance.  John
>--


You could have DOLLARS and CENTS defined as JUST RIGHT in working
storage or:

INSPECT PRICE
    REPLACING ALL SPACES BY ZEROS
UNSTRING PRICE DELIMITED BY '.'
    INTO DOLLARS
        CENTS
INSPECT DOLLARS
    REPLACING LEADING ZEROS BY SPACES
STRING DOLLARS DELIMITED BY SIZE
    CENTS DELIMITED BY SIZE
        INTO NEW-PRICE

Try one and see what happens,
Boyce G. Williams, Jr.

 .---------------------------------------------------------------------.
 | "People should have two virtues:  purpose- the courage to envisage  |
 | and pursue valued goals uninhibited by the defeat of infantile      |
 | fantasies, by guilt and the failing fear punishment;  and wisdom- a |
 | detached concern with life itself, in the face of death itself."    |
 |                                                     Norman F. Dixon |
 '---------------------------------------------------------------------'



Mon, 15 Nov 1999 03:00:00 GMT  
 String and Unstring



Quote:



> > > Hi, could anyone tell me how to deal with following problem:

> > >  01 price     pic x(6).
> > >  01 new-price pic x(5).

> Move 5 to new-price-pointer
> Move spaces to new-price
>  Perform with test after varying tally from 6 by -1 until tally = zero
>    if price (tally:1) numeric
>      move price (tally:1) to new-price (new-price-pointer:1)
>      subtract 1 from new-price-pointer
>    end-if
>  end-perform
> .

Did you find my bug?  It should be perform with test after until...... = 1
and not ZERO!


Mon, 15 Nov 1999 03:00:00 GMT  
 String and Unstring


Quote:


> >Hi, could anyone tell me how to deal with following problem:

> >       01 price     pic x(6).
> >       01 new-price pic x(5).

> >and data looks like this: 9.25   19.43   350.53. I want to remove the dot
> >so that data will look like ths:925   1943  35053.
snip.

> You could have DOLLARS and CENTS defined as JUST RIGHT in working
> storage or:

> INSPECT PRICE
>     REPLACING ALL SPACES BY ZEROS
> UNSTRING PRICE DELIMITED BY '.'
>     INTO DOLLARS
>         CENTS
> INSPECT DOLLARS
>     REPLACING LEADING ZEROS BY SPACES
> STRING DOLLARS DELIMITED BY SIZE
>     CENTS DELIMITED BY SIZE
>         INTO NEW-PRICE

snip.

Very close, but no cigar....

The JUSTIFIED RIGHT is the most important part...your code could very
simply look like:

01  OLD-PRICE       PIC X(6).
01  NEW-PRICE.
    05  DOLLARS     PIC X(3) JUSTIFIED RIGHT.
    05  CENTS       PIC X(2).

INITIALIZE NEW-PRICE.
UNSTRING OLD-PRICE DELIMITED BY '.' INTO DOLLARS CENTS.
INSPECT NEW-PRICE REPLACING ALL SPACES BY ZEROES.      

I tested this and it works every time...here's some displays from my
test program:

OLD PRICE = "1.23  "                
NEW PRICE = "  123" (AFTER UNSTRING)
NEW PRICE = "00123" (AFTER INSPECT)  

OLD PRICE = "123.45"                
NEW PRICE = "12345" (AFTER UNSTRING)
NEW PRICE = "12345" (AFTER INSPECT)  

OLD PRICE = ".67   "                
NEW PRICE = "   67" (AFTER UNSTRING)
NEW PRICE = "00067" (AFTER INSPECT)  

OLD PRICE = "8.9   "                
NEW PRICE = "  89 " (AFTER UNSTRING)
NEW PRICE = "00890" (AFTER INSPECT)  

Cheers :-)
--

Manager, Operations and Tech Support
United Retail Group. Inc.
Rochelle Park, NJ
(visit my meager web-site at http://mypage.ihost.com/jimswebsite/)



Mon, 15 Nov 1999 03:00:00 GMT  
 String and Unstring



Quote:
> 01  OLD-PRICE       PIC X(6).
> 01  NEW-PRICE.
>     05  DOLLARS     PIC X(3) JUSTIFIED RIGHT.
>     05  CENTS       PIC X(2).

> INITIALIZE NEW-PRICE.
> UNSTRING OLD-PRICE DELIMITED BY '.' INTO DOLLARS CENTS.
> INSPECT NEW-PRICE REPLACING ALL SPACES BY ZEROES.      

> I tested this and it works every time...here's some displays from my
> test program:

Jim, I liked your solution better than mine, and I learned how to get that
Justified Right to work.  I never could before.  Thanks!  One note though,
if the decimal point is omitted your example fails.
:(.  Thanks though, this is fun.


Mon, 15 Nov 1999 03:00:00 GMT  
 String and Unstring

Quote:

> Hi, could anyone tell me how to deal with following problem:

>         01 price     pic x(6).
>         01 new-price pic x(5).

> and data looks like this: 9.25   19.43   350.53. I want to remove the dot
> so that data will look like ths:925   1943  35053. I tried unstring
>         UNSTRING PRICE DELIMIETED BY '.'
>                 INTO DOLLARS
>                           CENTS
>         STRING DOLLARS DELIMITED BY SIZE
>                         CENTS DELIMITED BY SIZE
>                 INTO NEW-PRICE
> where dollars has pic x(3), cents pic x(2). The problem is that characters
> in DOLLARS are left justified, therefore 9.25 become 9--25 instead of
> --925 in new-price. Is there a way that you can right justify characters in
> an alphanumeric field? Thanks in advance.  John
> --


Why not just move the old-price to a pic 999v99 numeric edited field?

Joe



Mon, 15 Nov 1999 03:00:00 GMT  
 String and Unstring

Quote:

> Hi, could anyone tell me how to deal with following problem:

>         01 price     pic x(6).
>         01 new-price pic x(5).

> and data looks like this: 9.25   19.43   350.53. I want to remove the dot
> so that data will look like ths:925   1943  35053. I tried unstring
>         UNSTRING PRICE DELIMIETED BY '.'
>                 INTO DOLLARS
>                           CENTS
>         STRING DOLLARS DELIMITED BY SIZE
>                         CENTS DELIMITED BY SIZE
>                 INTO NEW-PRICE
> where dollars has pic x(3), cents pic x(2). The problem is that characters
> in DOLLARS are left justified, therefore 9.25 become 9--25 instead of
> --925 in new-price. Is there a way that you can right justify characters in
> an alphanumeric field? Thanks in advance.  John
> --


The new "de-editting" move statements that came with release 3 of COBOL
II allow        
you to code the following

     01 price pic zzz.99.
     01 new-price pic 999v99.

     Move price to new-price.

Does this work on your compiler ?

Colin Lucas



Wed, 17 Nov 1999 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. HELP: STRING and UNSTRING commands

2. string and unstrung

3. STRING, UNSTRING, and INSPECT under CICS under VSE

4. Unstring,Add Chars,Then String ?

5. [Fwd: STRING, UNstring & INSPECT are not recommended]

6. Help with string/unstring/inspect

7. How does one UNSTRING?

8. Parsing a Common Log Record Without Unstring

9. unstring

10. UNSTRING DELMITED BY ALL SPACES, SPACES?

11. Better way to unstring...?

12. Unstringing Name

 

 
Powered by phpBB® Forum Software