×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

unsintring in a loop?

unsintring in a loop?

unsintring in a loop?

(OP)
Hi all,

how can I perform an UNSTRING in a loop?

For example:

I have a varible WS-MY-STRING PIC X(100) value "AB/C/D/EFGH/I"-
WS-TMP is my destination variable (PIC X(100)).

I would like to have a loop to extract a different part of the string:

1. first time I would get "AB" into a WS-TMP
2. second time in a loop would return "C" in a WS-TMP.
then: "D", "EFGH" and finally "I"

Number of delimiters ("/") varies of course, length of each part between a delimiter varies, too.

I prefer an ANSI 85 solution, I use Microsoft Cobol 5.0 (DOS version), made by MicroFocus.

Is it possible to do this with an UNSTRING command or should I use a PERFORM?

Thanks for your help.
Saso

 

RE: unsintring in a loop?

(OP)
Thanks for your answer.

Well, I tried UNSTRING with a POINTER. But this does not do what I want to do.

I want to UNSTRING variable number of tokens from a string.

CODE

      $set ans85
       identification division.
       program-id.    abcd.
       working-storage section.
    01 strtmp    pic x(100).
    01 untmp    pic x(100).
    01 ws-pointer    pic 999.

       procedure division.
       p-d-z.
    move "AB/C/D/EFGH/I" to strtmp.

    move 1 to ws-pointer.
    unstring strtmp delimited by "/"
        into untmp
        with pointer ws-pointer
    end-unstring.
    display "1 " untmp.

    move 2 to ws-pointer
    unstring strtmp delimited by "/"
        into untmp
        with pointer ws-pointer
    end-unstring.
    display "2 " untmp.
    stop run.

If the code above would return "AB" and "C" then I would add a loop. But going thru the reference manual I just cannot find out if this is possible.
How can I (if possible) tell the UNSTRING statement to return 1st token, 2nd token etc. if the number of tokens is not known?

I can give you an example in an OpenVMS DCL:

CODE

$ strtmp="AB/C/D/EFGH/I"
$ cnt=0
$0:
$ untmp=f$element('cnt',"/",strtmp)
$ if untmp .eqs. "" .or. untmp .eqs. "/" then exit
$ write sys$output 'untmp'
$ cnt=cnt+1
$ goto 0

This would display

AB
C
D
EFGH
I

Hope this makes any sense.

Thanks,
Saso

P.S. Sorry for mistyped subject - tried to correct but I just can't find an EDIT button.

RE: unsintring in a loop?

A starting point:

CODE

identification division.
program-id.    tst.
working-storage section.
   01 strtmp    pic x(100).
   01 untmp    pic x(100).
   01 ws-pointer    pic 999.
   01 I pic 99.

procedure division.
p-d-z.
   move "AB/C/D/EFGH/I" to strtmp
   move 1 to ws-pointer I
   perform until ws-pointer > 100
     unstring strtmp delimited by "/"
       into untmp
       with pointer ws-pointer
     end-unstring
     display I " " untmp
     add 1 to I
   end-perform
   stop run.

Hope This Helps, PH.
FAQ219-2884: How Do I Get Great Answers To my Tek-Tips Questions?
FAQ181-2886: How can I maximize my chances of getting an answer?

RE: unsintring in a loop?

Actually, having played with this problem before, I would just consider UNSTRINGing this in one statement into a table and then process the table afterwards.  Seems to be less trouble that way.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.

RE: unsintring in a loop?

Something like
assuming UNSTRING not bothered by ref mod

CODE

move "AB/C/D/EFGH/I" to strtmp.

move 1 to ws-pointer.
Perform until ws-pointer > length of strtmp
    move high-values to untmp
    unstring strtmp (ws-pointer:) delimited by "/"
        into untmp
        with pointer ws-pointer
    end-unstring
    if strtmp not = high-values
        continue
    end-if
end-perform

 

www.moldyoldy.com/VBComparedemo - free online Compare/Diff of snippets

RE: unsintring in a loop?

You can also move or redefines your WS-MY-STRING into an array and do a perform. example:

Working-Storage:

02 WS-MY-STRING PIC X(100) value "AB/C/D/EFGH/I"
02 MY-ARR REDEFINES WS-MY-STRING.
   03 MY-ARRY OCCURS 100 TIMES.
      04 MY-ARRAY    PIC X(01).
02 X                 PIC 9(03) VALUE ZEROES.
02 Y                 PIC 9(03) VALUE ZEROES.
02 Z                 PIC 9(03) VALUE ZEROES.

Procedure division:
   ADD 1 TO Z.
   PERFORM VARYING X FROM 1 BY 1
     UNTIL (X >= 100)
        MOVE X TO Y
        IF (MY-ARRAY(X) = "/")
           SUBTRACT 1 FROM Y
           MOVE MY-ARRAY(Z:Y)  TO WS-TMP
           COMPUTE Z = X + 1
        END-IF
   END-PERFORM.

RE: unsintring in a loop?

More...

You may have to account what is the last character of your
WS-MY-STRING.

UNTIL (X >= 100)
   or (MY-ARRAY(X) = " ").... the perform will stop when a blank is encountered.

RE: unsintring in a loop?

(OP)
First, thank you all. I will check all these possibilites tomorrow (because of a time difference). I will post my results tomorrow.

Again, thank you all.
Saso

RE: unsintring in a loop?

Far too many years ago than I care to remember, I had a similar problem and I solved it by determining what the maximum number of elements there could possibly by in the field to be unstrung, and writing an unstring statement along the following lines:

CODE

UNSTRING WS-MY-STRING DELIMITED BY '/' INTO
WS-STRING-1 DELIMITER IN WS-DLIM-1,
WS-STRING-2 DELIMITER IN WS-DLIM-2,
WS-STRING-3 DELIMITER IN WS-DLIM-3,
ETC.
WS-STRING-1/2/3 etc. were elements in a redefined table that I could then access with a subscript.

Not sure if this helps or is as good as some of the suggestions above, but thought I'd post it as an alternative solution.

Marc

RE: unsintring in a loop?

(OP)
I checked all the versions you provided.
I will use the first solution (PHV's solution).
I see I was not far from the solution myself. Mistake I made was adding 1 to the UNSTRING POINTER. I didn't know that it gets updated automaticaly.
If I remove

CODE

move 2 to ws-pointer

from my code it works, too.

Again, thank you all.

Saso
 

RE: unsintring in a loop?

I like PHV's solution.  It is more flexible than UNSTRING'ing into a table since it does not require you to know in advance the maximum number tokens that can be expected.

One further suggestion: add an "OR ALL SPACES" to the DELIMITED BY clause.  This will ensure that the last token (from the example in the O/P) is identified as "I" and not "I                                        ". Not terribly important in the context of the examples given above, but generally this solution is the building block for something more complicated, and in those cases, generally knowing you have just "I" and not "I" followed by bunch of spaces can save you from having to also figure out how many spaces you have to ignore (even if it is easy to calculate).

There are some excellent past examples in this forum for dealing with the UNSTRING verb. If you click on the "Search" tab above and search for "UNSTRING" you will find them. [thread209-1520795: Removing spaces form a Message] seems particularly relevant to the current topic.

Code what you mean,
and mean what you code!
But by all means post your code!

Razalas

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close