Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parsing a character string 1

Status
Not open for further replies.

mpau

Programmer
Apr 10, 2009
1
FR
Hi all,

Which is the best (simple) way to do the following

input string
string = 'àDD!cli_ver!àTT!cli_org'

and i need to obtain the following outputs :

arg1 = 'DD'
arg2 = 'cli_ver'
arg3 = 'TT'
arg4 = 'cli_org'

regards
MPAU
 
For example this way:
split.rex
Code:
input_string [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'aDD!cli_ver!aTT!cli_org'[/color]
[COLOR=#804040][b]say[/b][/color] [COLOR=#ff00ff]"original string = '"[/color][COLOR=#804040][b]||[/b][/color]input_string[COLOR=#804040][b]||[/b][/color][COLOR=#ff00ff]"'"[/color]

[COLOR=#0000ff]/* replace characters 'a' and '!' with spaces */[/color]
str[COLOR=#804040][b]=[/b][/color][COLOR=#008080]translate([/color]input_string[COLOR=#804040][b],[/b][/color][COLOR=#ff00ff]'  '[/color][COLOR=#804040][b],[/b][/color][COLOR=#ff00ff]'!a'[/color][COLOR=#008080])[/color]
[COLOR=#804040][b]say[/b][/color] [COLOR=#ff00ff]"modified string = '"[/color][COLOR=#804040][b]||[/b][/color]str[COLOR=#804040][b]||[/b][/color][COLOR=#ff00ff]"'"[/color]

[COLOR=#0000ff]/* compute number of words */[/color]
nr_words [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]words([/color]str[COLOR=#008080])[/color]
[COLOR=#804040][b]say[/b][/color] [COLOR=#ff00ff]"modified string consists of"[/color] nr_words [COLOR=#ff00ff]"words"[/color]

[COLOR=#0000ff]/* extract words to stem */[/color]
words_stem.0 [COLOR=#804040][b]=[/b][/color] nr_words
[COLOR=#804040][b]do[/b][/color] j[COLOR=#804040][b]=[/b][/color]1[COLOR=#804040][b] to [/b][/color]words_stem.0
  words_stem.j [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]word([/color]str[COLOR=#804040][b],[/b][/color]j[COLOR=#008080])[/color]
[COLOR=#804040][b]end[/b][/color]

[COLOR=#0000ff]/* list the words found */[/color]
[COLOR=#804040][b]do[/b][/color] j[COLOR=#804040][b]=[/b][/color]1[COLOR=#804040][b] to [/b][/color]words_stem.0
  [COLOR=#804040][b]say[/b][/color] [COLOR=#ff00ff]"word"[/color][COLOR=#804040][b]||[/b][/color][COLOR=#008080]right([/color]j[COLOR=#804040][b],[/b][/color]2[COLOR=#804040][b],[/b][/color]0[COLOR=#008080])[/color][COLOR=#804040][b]||[/b][/color][COLOR=#ff00ff]" = '"[/color][COLOR=#804040][b]||[/b][/color]words_stem.j[COLOR=#804040][b]||[/b][/color][COLOR=#ff00ff]"'"[/color]
[COLOR=#804040][b]end[/b][/color]
[COLOR=#804040][b]exit[/b][/color]

Output:
Code:
$ rexx split.rex
original string = 'aDD!cli_ver!aTT!cli_org'
modified string = ' DD cli_ver  TT cli_org'
modified string consists of 4 words
word01 = 'DD'
word02 = 'cli_ver'
word03 = 'TT'
word04 = 'cli_org'
 
I like the first approach.

This would be a possible alternative:

string = 'àDD!cli_ver!àTT!cli_org'
Parse Var string 'à' arg1 '!' arg2 '!à' arg3 '!' arg4 cli_org .

Say arg1
Say arg2
Say arg3
Say arg4
 
However the first aproach is more universal. It's not depending of number of words in the string.
For example, if you change the input string to
Code:
input_string = 'aDD1!cli_ver1!aTT1!cli_org1a!DD2!cli_ver2!aTT2!cli_org2'
you don't need change nothing in the code and it works:
Code:
original string = 'aDD1!cli_ver1!aTT1!cli_org1a!DD2!cli_ver2!aTT2!cli_org2'
modified string = ' DD1 cli_ver1  TT1 cli_org1  DD2 cli_ver2  TT2 cli_org2'
modified string consists of 8 words
word01 = 'DD1'
word02 = 'cli_ver1'
word03 = 'TT1'
word04 = 'cli_org1'
word05 = 'DD2'
word06 = 'cli_ver2'
word07 = 'TT2'
word08 = 'cli_org2'
With the second approach, if you change the input string, then you will need to change the parse var statement too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top