View previous topic :: View next topic |
Author |
Message |
John Corbin Beginner
Joined: 23 Jan 2004 Posts: 38 Topics: 21
|
Posted: Wed Feb 04, 2004 1:43 pm Post subject: Stepping through a string charcter by character |
|
|
Hi...
In REXX I need to test each character in a string and do some action based on the outcome of the test.
in english it would like this:
WHILE not end of string DO
IF string[I] = 'A'
perform some action
ELSE IF string[I] = 'B'
perform sone action
ELSE IF string[I] = 'C'
etc etc....
I=I+1
ENDWHILE
I am aware of the REXX functiosn INDEX and POS but I do not see how I can make these work they way I want.
Any thoughts ? |
|
Back to top |
|
 |
ofer71 Intermediate
Joined: 12 Feb 2003 Posts: 358 Topics: 4 Location: Israel
|
Posted: Wed Feb 04, 2004 2:47 pm Post subject: |
|
|
Try this
Code: | /* REXX */
STR = 'TEST'
DO I = 1 TO LENGTH(STR)
CHAR = SUBSTR(STR,I,1)
SELECT
WHEN CHAR = 'A' THEN
do something
WHEN CHAR = 'B' THEN
do something else
OTHERWISE
NOP
END
END
EXIT |
O.
________
California Medical Marijuana Dispensary
Last edited by ofer71 on Thu Mar 17, 2011 10:37 am; edited 1 time in total |
|
Back to top |
|
 |
Manas Biswal Intermediate

Joined: 29 Nov 2002 Posts: 382 Topics: 27 Location: Chennai, India
|
Posted: Thu Feb 05, 2004 11:09 am Post subject: |
|
|
Basically reinventing the wheel by Ofer71 but just to try another function, you can also try this -
Code: |
/***** rexx ******/
var1 = 'manas'
restvar = var1
do i = 1 to length(var1)
parse var restvar var2 2 restvar
/* Do whatever you want to do with var2 here */
end
exit
|
Regards,
Manas |
|
Back to top |
|
 |
Maton_Man Beginner

Joined: 30 Jan 2004 Posts: 123 Topics: 0
|
Posted: Mon Feb 09, 2004 6:02 pm Post subject: |
|
|
A sneakier way to do it without a massive SELECT statement would be:
var = "ACBDE"
do i = 1 to length(var)
interpret("call "left(var,1))
end
a:
whatever you do when you get 'A'
return
b:
return
whatever you do when you get 'B'
etc... _________________ My opinions are exactly that. |
|
Back to top |
|
 |
Maton_Man Beginner

Joined: 30 Jan 2004 Posts: 123 Topics: 0
|
Posted: Mon Feb 09, 2004 6:05 pm Post subject: |
|
|
*argh!!!!*
Replace left(var,1) with substr(var,i,1) - got distracted... _________________ My opinions are exactly that. |
|
Back to top |
|
 |
|
|