IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Need to find the starting position of the 2nd word


IBM Mainframe Forums -> PL/I & Assembler
Post new topic   Reply to topic
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
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

Moderator Emeritus


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

PostPosted: Mon May 31, 2010 9:24 pm
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
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
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

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Tue Jun 01, 2010 11:24 am
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
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

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Tue Jun 01, 2010 11:57 am
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
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

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Tue Jun 01, 2010 12:53 pm
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
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: 8696
Location: Dubuque, Iowa, USA

PostPosted: Tue Jun 01, 2010 7:05 pm
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

Moderator Emeritus


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

PostPosted: Tue Jun 01, 2010 9:54 pm
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

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Wed Jun 02, 2010 12:16 am
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
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

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Thu Jun 03, 2010 12:36 pm
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
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

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Thu Jun 03, 2010 5:24 pm
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

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Thu Jun 03, 2010 5:34 pm
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

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Fri Jun 04, 2010 1:48 pm
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

Senior Member


Joined: 07 Feb 2009
Posts: 1306
Location: Vilnius, Lithuania

PostPosted: Fri Jun 04, 2010 2:58 pm
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
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Fri Jun 04, 2010 3:18 pm
Reply with quote

donateeye,

your code presupposes the knowledge of the first word in the string.

My interpretation of Sundardudos's requirement is:

find the position of the second word in any string,

I find that enrico's solutions provide that info
Actually, they provide a complete answer to an unasked question:

what are the starting and ending positions of all words in a string.

If Sundardudos only wants to know the attributes of the second word,
he can modify enrico's code.

using your code, you would have to add a routine to run previous that
would extract the first word.

Now, analyzing the complete string (all the words),
is something that is often done -
but, if for some reason, the TS only wants the 2nd,
truncating enrico's code will provide the info.
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Fri Jun 04, 2010 3:20 pm
Reply with quote

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


because the manual version I was looking at did not have the 3 args version

and I would have had anyway to use a trick 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: Fri Jun 04, 2010 4:47 pm
Reply with quote

Yes, Sundarkudos has clear said

If the first word in the source is xxxxxx then...

which clearly means that he should be aware of the first word...

considering that, the code just needs to find only the starting position of the second word.

Interesting is that, the man who started this topic is no where here to read all the suggestions and codes for him....
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Fri Jun 04, 2010 5:00 pm
Reply with quote

donateeye,

you are pounding sand.

one assumes the program is to handle more than one input,
thus the first word is not always known.

enrico and the other seniors do not write code for simple accept/display cobol programs.
their examples are an attempt to drag those
with little experience
out of the dark chasm of ignorance that they seem to reside
and provide insight to the world of professional programming.
Back to top
View user's profile Send private message
donateeye
Warnings : 2

New User


Joined: 01 Jun 2007
Posts: 62
Location: chennai

PostPosted: Fri Jun 04, 2010 5:12 pm
Reply with quote

well.... you are right.... My apologize to enrico....
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> PL/I & Assembler Goto page 1, 2  Next

 


Similar Topics
Topic Forum Replies
No new posts PARSE Syntax for not fix length word ... JCL & VSAM 7
No new posts INCLUDE OMIT COND for Multiple values... DFSORT/ICETOOL 5
No new posts Search two or more word with FILEAID Compuware & Other Tools 15
No new posts To find whether record count are true... DFSORT/ICETOOL 6
No new posts Find the size of a PS file before rea... COBOL Programming 13
Search our Forums:

Back to Top