MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Search all the occurences of a character in a word in REXX !

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> TSO and ISPF
View previous topic :: View next topic  
Author Message
amit4u79
Beginner


Joined: 24 Oct 2005
Posts: 109
Topics: 36
Location: India

PostPosted: Tue Nov 08, 2005 8:53 pm    Post subject: Search all the occurences of a character in a word in REXX ! Reply with quote

Hi techfundoos..
I am having to code a REXX which would find the position of all occurences of a particular character in a string.
For example suppose the string is "ABXCDXDDDXEEEXGGGX", then I need to find out the position of X in the string. The output should give me something like "X is at positions 3,6,10,14,18.
I cannot use POS builtin func as that would give me only first occuring position number of X. Also, I can manipulate POS func, but I was looking if there was some straightforward way to do this.

Thanks, kindly help.

Amit Joshi
Singapore.
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 700
Topics: 63
Location: USA

PostPosted: Wed Nov 09, 2005 5:08 am    Post subject: Reply with quote

Don't know if it is simple or not but here is an untested approach -

Code:
my_string = "ABXCDXDDDXEEEXGGGX"
my_char  = "X"
my_message = my_char||" is at positions"

if index(my_string,my_char) = 0 then
    my_message = my_message||' 0'
else
    do m = 1 to length(my_string)
         if substr(my_string,m,1) = my_char then
         my_message = my_message||' 'm
    end   

This would give an output like "X is at positions 3 6 10 14 18"

Regards,
Diba.
Back to top
View user's profile Send private message Send e-mail
danm
Intermediate


Joined: 29 Jun 2004
Posts: 170
Topics: 73

PostPosted: Wed Nov 09, 2005 8:58 am    Post subject: Reply with quote

Code:

/* REXX */
String = 'ABXCDXDDDXEEEXGGGX'
match_char = 'X'
positions = ''

Start_pos = 1
Do until idx = 0
  idx = pos(match_char,String,Start_pos)
  If idx <> 0 then positions = positions idx
  Start_pos = idx + 1
End  /* Do until idx = 0 */
say 'character string =' String
If positions = '' then say 'Character' match_char 'not found'
Else say 'Character' match_char 'found in :' space(positions,1,',')
Exit
Back to top
View user's profile Send private message
amit4u79
Beginner


Joined: 24 Oct 2005
Posts: 109
Topics: 36
Location: India

PostPosted: Thu Nov 10, 2005 1:20 am    Post subject: Reply with quote

Hey thanks a lot for giving me the code for my requirement. It really helped save a lot of time and effort on my part.

Thanks again,
Amit Joshi
Singapore
_________________
I did not fail; I have found 10,000 ways that would not work - Albert Einstein.
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> TSO and ISPF All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group