Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes

 Need to find the starting position of the 2nd word Goto page 1, 2  Next
Author Message
sundarkudos

New User

Joined: 16 Oct 2008
Posts: 39
Location: Chennai

 Posted: Mon May 31, 2010 4:15 pm    Post subject: Need to find the starting position of the 2nd word Hi, A requirement goes like this. ABC 1ABC 2ABC 3ABC If the first word in the line is 'ABC', then i need to find the starting position of the 2nd word.It can have any number of spaces in between 1st and 2nd word. Is there any function in PLI to make use for it??? Please help. Thanks, Sundar.

dick scherrer

Site Director

Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

 Posted: Mon May 31, 2010 9:24 pm    Post subject: Hello, What is the "second word" to you? 1ABC or the second occurrence of ABC?
sundarkudos

New User

Joined: 16 Oct 2008
Posts: 39
Location: Chennai

 Posted: Tue Jun 01, 2010 8:40 am    Post subject: Hi Dick, The position of '1' in the word '1ABC'. Thanks, Sundar.
donateeye
Warnings : 2

New User

Joined: 01 Jun 2007
Posts: 62
Location: chennai

 Posted: Tue Jun 01, 2010 11:14 am    Post subject: Reply to: Need to find the starting position of the 2nd word S=SUBSTR(SOURCE,1,3); IF SUBSTR(S,1,3) = 'ABC' THEN DO; T=SUBSTR(SOURCE,LENGHT(S)+1,LENGTH(SOURCE)-LENGTH(S)); S1=TRIM(T,' '); K=INDEX(T,SUBSTR(S1,1,1); K=K+3; END; K IS YOUR ANSWER Example : ABC......ABCD...DEF S = ABC T = ......ABCD...DEF S1 = ABCD...DEF K = 7 K = 7 + 3 + 10 FINALLY K = 10 WHICH IS NOTHING BUT THE START OF THE SECOND WORD IN THE SOURCE (ABCD)
enrico-sorichetti

Global Moderator

Joined: 14 Mar 2007
Posts: 10219
Location: italy

 Posted: Tue Jun 01, 2010 11:24 am    Post subject: Reply to: Need to find the starting position of the 2nd word the code posted does not satisfy the request... it depends on the string "ABC" in the source a correct logic would keep a word count and alternating the search for a blank and non blank chars pseudo logic ... initialize the count, position, length position the string pointer at the beginning of the string find the first non blank char ( identifies the beginning of the next ) update the word count and position find the first blank ( identifies the end of the current ) process the current ( store the length) keep looping
donateeye
Warnings : 2

New User

Joined: 01 Jun 2007
Posts: 62
Location: chennai

 Posted: Tue Jun 01, 2010 11:47 am    Post subject: S=SUBSTR(SOURCE,1,3); IF S = 'ABC' THEN DO; T=SUBSTR(SOURCE,LENGHT(S)+1,LENGTH(SOURCE)-LENGTH(S)); S1=TRIM(T,' '); K=INDEX(T,SUBSTR(S1,1,1); K=K+3; END; the above code should work (apologize, there was a typo error in my previous post) Please advice me why the code does not satisfy the request.?
enrico-sorichetti

Global Moderator

Joined: 14 Mar 2007
Posts: 10219
Location: italy

 Posted: Tue Jun 01, 2010 11:57 am    Post subject: Reply to: Need to find the starting position of the 2nd word looks like You did not care to understand the original request and my comment ... ABC 1ABC and so on are just place holders and as said before Your code relies on the ABC being in the input string so do not insist on posting wrong code and meditate on my code
donateeye
Warnings : 2

New User

Joined: 01 Jun 2007
Posts: 62
Location: chennai

 Posted: Tue Jun 01, 2010 12:20 pm    Post subject: Yes, I was considering his example, here is the generic code THE CODE : M = MATCHING WORD S=SUBSTR(SOURCE,1,LENGTH(M)); IF M=S THEN DO; T=SUBSTR(SOURCE,LENGHT(S)+1,LENGTH(SOURCE)-LENGTH(S)); S1=TRIM(T,' '); K=INDEX(T,SUBSTR(S1,1,1); K=K+LENGTH(S); END; LETS CONSIDER THE FOLLOWING : M is the matching word, lets say it is 'APPLE' Lets have Source as 'APPLE....BOY.....CAN' (... ARE SPACES) How it works : M = APPLE 'AS GIVEN' S = APPLE M IS EQUAL TO S, SO GOES INTO THE LOOP T = ....BOY......CAN S1 = BOY......CAN K = 5 K = K + 5 FINALLY K = 10 WHICH IS NOTHING BUT THE START OF THE SECOND WORD IN THE SOURCE
enrico-sorichetti

Global Moderator

Joined: 14 Mar 2007
Posts: 10219
Location: italy

 Posted: Tue Jun 01, 2010 12:53 pm    Post subject: Reply to: Need to find the starting position of the 2nd word I think that You still miss the point... why do You insist in match the string about a known consider a string of arbitrary words with an arbitrary number of spaces use SUBSTR to find out the starting position of the next word ( first non blank ) use INDEX to find the end of a word ( first blank )
sundarkudos

New User

Joined: 16 Oct 2008
Posts: 39
Location: Chennai

Robert Sample

Global Moderator

Joined: 06 Jun 2008
Posts: 7953
Location: Bellevue, IA

Posted: Tue Jun 01, 2010 7:05 pm    Post subject:

 Code: IF UPPERCASE(SUBSTR(TRIM(STRINGVAR),1,3)) = 'ABC' THEN DO;      ABCLOC        = INDEX(UPPERCASE(STRINGVAR),'ABC') ;      FIRSTCHAR     = SUBSTR(TRIM(SUBSTR(STRINGVAR,ABCLOC+3)),1,1) ;      SECONDWORDLOC = INDEX(SUBSTR(STRINVAR,4),FIRSTCHAR)+3 ;      END;
I haven't tested this, so beware. You need to verify that the first word is ABC. Once that is done, find its location in the string (leading blanks may mean it does not start in the first position). Find the first character of the second word. Find the location of that character in the string -- SECONDWORDLOC is that starting position.
dick scherrer

Site Director

Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

Posted: Tue Jun 01, 2010 9:54 pm    Post subject:

Hello,

Possibly because the requirement as posted has confused more than it has aided in getting a solution. . .

Suggest you show multiple input lines with differing values and explain the "answer" you want when each set is processed. Use the Code tag to preserver alignment and a "ruler" to show data positions:
 Code: ----+----1----+----2----+----3----+----4----+----5
enrico-sorichetti

Global Moderator

Joined: 14 Mar 2007
Posts: 10219
Location: italy

Posted: Wed Jun 02, 2010 12:16 am    Post subject: Reply to: Need to find the starting position of the 2nd word

 Quote: I'm screwed.

all depends on who is holding the screwdriver
donateeye
Warnings : 2

New User

Joined: 01 Jun 2007
Posts: 62
Location: chennai

 Posted: Thu Jun 03, 2010 10:02 am    Post subject: Sundarkudos.... tell me whether you tried the code that I posted ???? and let me know what do you expect and what did my code deliver ??? for your reference, this is the code I am referring to : M = MATCHING WORD /* in your example, this was 'ABC' */ S=SUBSTR(SOURCE,1,LENGTH(M)); IF M=S THEN DO; T=SUBSTR(SOURCE,LENGHT(S)+1,LENGTH(SOURCE)-LENGTH(S)); S1=TRIM(T,' '); K=INDEX(T,SUBSTR(S1,1,1); K=K+LENGTH(S); END; Please try this out and let me if you are not getting what you expected..?
enrico-sorichetti

Global Moderator

Joined: 14 Mar 2007
Posts: 10219
Location: italy

Posted: Thu Jun 03, 2010 12:36 pm    Post subject: Reply to: Need to find the starting position of the 2nd word

the code is wrong in my way of reading the topic because it relies on the presence of a matching as You call it word

try to run it without change on two different strings
 Code: "word1      word2 wrord3 ..." "someotherword1  adifferentword2 asimpleword3"

I just reread the whole topic and ....

maybe there is a language/phylosophical/way of reading things barrier here,
mostly phylosophical I guess

given a string find the starting position of the second/any word
the algorithm is general an useful to everybody

the two things are examples and the "ABC" is a placeholder
I guess that this way is the most reasonable ( but still my opinion )
to specify that the data to be processed was EXACTLY and ONLY the data posted
in this is the case Your algorithm might work

but I feel that my interpretation is the right one because the TS said
 Quote: A requirement goes like this
and he was trying to explain to us dummies what he mean by <word>

I have some personal urgencies right now ... NO IS NOT WHAT YOU THINK
I' ll post later a code snippet which given a string will parse it and build trhee arrays
words array
length array
poisition array
donateeye
Warnings : 2

New User

Joined: 01 Jun 2007
Posts: 62
Location: chennai

 Posted: Thu Jun 03, 2010 2:45 pm    Post subject: You are right... Perhaps, the ideas werent communicated perfectly. According to sundarkudos, he has a character value stored in a variable (lets consider M as the variable name, and the character value is any group of characters for eg, 'apple' or 'childhood' etc) Also, he has another variable 'S' where group of words (like a sentence) is stored. eg; S has the value 'apple tree is good' OR it might have the value 'orange tree is good'. Now, all he have is these variables : M (in our example, M is assigned the character value 'apple') S (in our example, S is assigned the character value 'apple tree is good') His requirement is : IF the value in M is the first word in S, THEN find the starting position of the second word (in our example, find the position of 't' in the variable S) ELSE ignore everything. The code which I posted first checks whether M is the first word in S (assuming we declare M as char varying, so the whole of M should be the first word in S, inorder for the process to continue) then does the rest. so, if we have 'orange' in M, then this would go to the ELSE part and does nothing. If we have 'apple' in M, and if we had different number of spaces between the first and second word in S, then too this code would give the position of where the second word begins. Sundarkudos....!! hope you are there reading this. Please correct me, and also try the code, I couldnt see you popping in for long time.
enrico-sorichetti

Global Moderator

Joined: 14 Mar 2007
Posts: 10219
Location: italy

Posted: Thu Jun 03, 2010 5:24 pm    Post subject: Reply to: Need to find the starting position of the 2nd word

I will not have access to a PL/I compiler for a while..
here is a prototype written in rexx...
the PL/I coding might follow the same

 Code: str = "someword    anotherword     againaword" wc = 0 /* word count */ ws = 0 /* word start , works also as an indicator of a word found */ len = length(str) do   i = 1 to len    if   ws = 0 then do       if   substr(str,i,1) = " " then ,          iterate             ws = i       wc = wc + 1    end    else do       if   substr(str,i,1) \= " " then ,          iterate             wl = i - ws       say wc wl substr(str,ws,wl)       ws = 0    end end if   ws > 0 then do    wl = i - ws       say wc wl substr(str,ws,wl) end
enrico-sorichetti

Global Moderator

Joined: 14 Mar 2007
Posts: 10219
Location: italy

Posted: Thu Jun 03, 2010 5:34 pm    Post subject: Reply to: Need to find the starting position of the 2nd word

and here is another prototype
using the POS/INDEX builtin function for the terminating blank
instead of substringing char by char

 Code: str = "someword    anotherwor     againaword      " wc = 0 ws = 0 len = length(str) i = 1 do   while ( i < len )    if   substr(str,i,1) = " " then do       i = i + 1       iterate          end    ws = i    wc = wc + 1    we = pos(" ",str,i)    if   we = 0 then       we = len +   1    wl = we - ws    say wc wl substr(str,ws,wl)    ws = 0    i = we end
enrico-sorichetti

Global Moderator

Joined: 14 Mar 2007
Posts: 10219
Location: italy

Posted: Fri Jun 04, 2010 1:48 pm    Post subject: Reply to: Need to find the starting position of the 2nd word

and here is a quick and dirty PLI for it

CODE:
 Code: STRPARS: PROC OPTIONS(MAIN);     DCL S CHAR(80) ;  DCL (I,L) FIXED BIN(31) ;  DCL (WC, WS, WE, WL ) FIXED BIN(31);  S = '      thisisword1 someword2 anotherword3' ;  L = LENGTH(S) ;  PUT SKIP LIST(L,S) ;  WC = 0 ;  WS = 0 ;  I  = 1 ;  DO  WHILE ( I < L );            IF  SUBSTR(S,I,1) = ' ' THEN          I = I + 1 ;      ELSE DO ;          WS = I ;          WC = WC + 1 ;          WE = INDEX(SUBSTR(S,WS),' ') ;          IF  WE = 0 THEN WE = L + 1 ;                     ELSE WE = WE + WS ;            WL = WE - WS - 1 ;          PUT SKIP LIST (WC, WS, WL, SUBSTR(S,WS,WL)) ;          WS = 0 ;          I = WE ;         END ;   END ;  END STRPARS;

RESULT:
 Code: 80                thisisword1 someword2 anotherword3              1                       7                      11          thisisword1              2                      19                       9          someword2              3                      29                      12          anotherword3
prino

Active Member

Joined: 07 Feb 2009
Posts: 997
Location: Oostende, Belgium

 Posted: Fri Jun 04, 2010 2:58 pm    Post subject: Why doesn't anyone mention "VERIFY", now with the option of starting in the middle of the string? Sheesh...
 All times are GMT + 6 HoursGoto page 1, 2  Next
 Page 1 of 2

Search our Forum:

 Topic Author Forum Replies Posted Similar Topics find particular member name in PDS us... ravi243 CLIST & REXX 10 Mon Dec 19, 2016 6:44 pm How to find the first monday of the w... abdulrafi COBOL Programming 10 Fri Nov 25, 2016 3:24 pm Find out a active PGM jpsager JCL & VSAM 10 Fri Oct 07, 2016 4:32 pm How to find a CICS resource used in C... Arunkumar Chandrasekaran CICS 8 Thu Sep 29, 2016 1:45 pm Online Assembler Program Starting point Aditya.Srivastava PL/I & Assembler 4 Fri Jul 08, 2016 6:48 pm

 © 2003-2016 IBM MAINFRAME Software Support Division
 Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us