Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Need to find the starting position of the 2nd word
Goto page 1, 2  Next
 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> PL/I & Assembler
View previous topic :: :: View next topic  
Author Message
sundarkudos

New User


Joined: 16 Oct 2008
Posts: 39
Location: Chennai

PostPosted: Mon May 31, 2010 4:15 pm    Post subject: Need to find the starting position of the 2nd word
Reply with quote

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.
Back to top
View user's profile Send private message

dick scherrer

Site Director


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

PostPosted: Mon May 31, 2010 9:24 pm    Post subject:
Reply with quote

Hello,

What is the "second word" to you? 1ABC or the second occurrence of ABC?
Back to top
View user's profile Send private message
sundarkudos

New User


Joined: 16 Oct 2008
Posts: 39
Location: Chennai

PostPosted: Tue Jun 01, 2010 8:40 am    Post subject:
Reply with quote

Hi Dick,

The position of '1' in the word '1ABC'.

Thanks,
Sundar.
Back to top
View user's profile Send private message
donateeye
Warnings : 2

New User


Joined: 01 Jun 2007
Posts: 62
Location: chennai

PostPosted: Tue Jun 01, 2010 11:14 am    Post subject: Reply to: Need to find the starting position of the 2nd word
Reply with quote

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)
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10201
Location: italy

PostPosted: Tue Jun 01, 2010 11:24 am    Post subject: Reply to: Need to find the starting position of the 2nd word
Reply with quote

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 <word> 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 <word>)
update the word count and position
find the first blank ( identifies the end of the current <word>)
process the current <word> ( store the length)
keep looping
Back to top
View user's profile Send private message
donateeye
Warnings : 2

New User


Joined: 01 Jun 2007
Posts: 62
Location: chennai

PostPosted: Tue Jun 01, 2010 11:47 am    Post subject:
Reply with quote

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.?
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10201
Location: italy

PostPosted: Tue Jun 01, 2010 11:57 am    Post subject: Reply to: Need to find the starting position of the 2nd word
Reply with quote

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 <thing> being in the input string

so do not insist on posting wrong code and meditate on my <pseudo> code
Back to top
View user's profile Send private message
donateeye
Warnings : 2

New User


Joined: 01 Jun 2007
Posts: 62
Location: chennai

PostPosted: Tue Jun 01, 2010 12:20 pm    Post subject:
Reply with quote

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
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10201
Location: italy

PostPosted: Tue Jun 01, 2010 12:53 pm    Post subject: Reply to: Need to find the starting position of the 2nd word
Reply with quote

I think that You still miss the point...
why do You insist in match the string about a known <thing>

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 )
Back to top
View user's profile Send private message
sundarkudos

New User


Joined: 16 Oct 2008
Posts: 39
Location: Chennai

PostPosted: Tue Jun 01, 2010 6:47 pm    Post subject:
Reply with quote

I'm screwed. icon_mad.gif Please help.

Thanks,
Sundar.
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


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

PostPosted: Tue Jun 01, 2010 7:05 pm    Post subject:
Reply with quote

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.
Back to top
View user's profile Send private message
dick scherrer

Site Director


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

PostPosted: Tue Jun 01, 2010 9:54 pm    Post subject:
Reply with quote

Hello,

Quote:
I'm screwed. Please help.
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
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10201
Location: italy

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

Quote:
I'm screwed.


all depends on who is holding the screwdriver icon_biggrin.gif
Back to top
View user's profile Send private message
donateeye
Warnings : 2

New User


Joined: 01 Jun 2007
Posts: 62
Location: chennai

PostPosted: Thu Jun 03, 2010 10:02 am    Post subject:
Reply with quote

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..?
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10201
Location: italy

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

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 )
otherwise the TS should have used an additional adjective like VERBATIM
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> icon_cool.gif

I have some personal urgencies right now ... NO IS NOT WHAT YOU THINK icon_biggrin.gif
I' ll post later a code snippet which given a string will parse it and build trhee arrays
words array
length array
poisition array
Back to top
View user's profile Send private message
donateeye
Warnings : 2

New User


Joined: 01 Jun 2007
Posts: 62
Location: chennai

PostPosted: Thu Jun 03, 2010 2:45 pm    Post subject:
Reply with quote

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.
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10201
Location: italy

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

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
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10201
Location: italy

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

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

Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10201
Location: italy

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

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
Back to top
View user's profile Send private message
prino

Active Member


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

PostPosted: Fri Jun 04, 2010 2:58 pm    Post subject:
Reply with quote

Why doesn't anyone mention "VERIFY", now with the option of starting in the middle of the string?

Sheesh...
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> PL/I & Assembler All times are GMT + 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts How to find the first monday of the w... abdulrafi COBOL Programming 10 Fri Nov 25, 2016 3:24 pm
No new posts Find out a active PGM jpsager JCL & VSAM 10 Fri Oct 07, 2016 4:32 pm
No new posts How to find a CICS resource used in C... Arunkumar Chandrasekaran CICS 8 Thu Sep 29, 2016 1:45 pm
No new posts Online Assembler Program Starting point Aditya.Srivastava PL/I & Assembler 4 Fri Jul 08, 2016 6:48 pm
No new posts how to find clist's or view library Andrew Hsia CLIST & REXX 4 Tue Jun 28, 2016 6:07 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us