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

Counting number of characters in a string


IBM Mainframe Forums -> CLIST & REXX
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Pragati Soni

New User


Joined: 18 Jan 2008
Posts: 47
Location: India

PostPosted: Thu May 21, 2009 7:35 pm
Reply with quote

I need to count number of '.' in a string like 'PRD.Y00205.REXX.QHTOOL.XXX.YYY'.

Actually I need to check if the first letter of every substring is an alphabet and neither number nor spaces. For this I need to seperate PRD, Y00205, REXX, QHTOOL, XXX, YYY.

Approach1:
I can PARSE the string, but the issue is the number of substrings are not known before hand.
I mean it can be
'PRD.Y00205.REXX.QHTOOL.XXX.YYY'
or
'PRD.Y00205.REXX.QHTOOL.XXX'
or
'PRD.Y00205.REXX.QHTOOL'
or
so on

So I am thinking to count the number of '.' and then the number of substrings can be +1. and I can easily parse the string.

Approach2:
I also tried to use translate func to replace '.' by spaces but it is not working for spaces.
QHTLLOCS = TRANSLATE('PRD.Y00205.REXX.QHTOOL.XXX.YYY','.',' ')
I thought of replacing the '.' by spaces and then counting the words delimited by space.

Please suggest something especially for Approach1.

Thanks in advance.
Back to top
View user's profile Send private message
acs_amit

New User


Joined: 30 Apr 2009
Posts: 14
Location: Noida

PostPosted: Thu May 21, 2009 7:49 pm
Reply with quote

Why don't you try to check first character like this.. I hope this works

Code:
ABC = 'PRD.Y00205.REXX.QHTOOL.XXX.YYY'                     
CHARAC = SUBSTR(ABC,1,1)                                   
SAY CHARAC                                                 
IF ((CHARAC = ' ') | (TRANSLATE(DATATYPE(CHARAC)) = 'NUM'))
THEN                                                       
    SAY 'FIRST CHARACTER IS NEITHER SPACES NOR NUMERIC'     
ELSE                                                       
    SAY 'FIRST CHARACTER MAY BE CHAR'                       


This is extracting first character of your input string and checking that it shouldn't be spaces or Numeric..
Translate function is used to make the input string in CAPS.

let me know if this helps
Back to top
View user's profile Send private message
Mickeydusaor

Active User


Joined: 24 May 2006
Posts: 258
Location: Salem, Oregon

PostPosted: Thu May 21, 2009 7:53 pm
Reply with quote

change your translate to this, switch the space and period around


QHTLLOCS = TRANSLATE('PRD.Y00205.REXX.QHTOOL.XXX.YYY',' ','.')
count = words(qhtllocs)
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


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

PostPosted: Thu May 21, 2009 7:56 pm
Reply with quote

Code:

rest_of_it = input_string
node_stem.0 = 0
Do i = 1 to 10
    parse var rest_of_it node_stem.i '.' rest_of_it
    if rest_of_it = '' then leave
    node_stem.0 = i
end


edited to correct some code.
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


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

PostPosted: Thu May 21, 2009 8:23 pm
Reply with quote

actually, ignore my previous post.

Code:

rest_of_it = input_string||'.'           <<<<<change here
node_stem.0 = 0
Do i = 1 to 10
    parse var rest_of_it node_stem.i '.' rest_of_it
    node_stem.0 = i                                         <<<<reversed
    if rest_of_it = '' then leave                      <<<<<<these lines
end
Back to top
View user's profile Send private message
Pedro

Global Moderator


Joined: 01 Sep 2006
Posts: 2547
Location: Silicon Valley

PostPosted: Thu May 21, 2009 9:41 pm
Reply with quote

Yes, it is unlikely, but there could be more than 10 levels.
Code:
Do i = 1 to 10



I prefer Mickey's translate and word approach. It seems cleaner to me.
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Thu May 21, 2009 10:15 pm
Reply with quote

standard approach to parse a dataset name


Code:
dsn = "AAA.DEEE.E33.R55.T66"
do  i = 1 while dsn <> ""
    parse var dsn hlq "." dsn
    hlq.i = hlq
    hlq.0 = i
end

do  i = 1 to hlq.0
    say right(i,2) hlq.i
end



edited to add a comment
Hi Dick!
sometimes the fingers are faster than the eyes icon_biggrin.gif
I had not seen that You had already posted the same solution
I just cut and pasted the code from my snippet collection!
Back to top
View user's profile Send private message
Pedro

Global Moderator


Joined: 01 Sep 2006
Posts: 2547
Location: Silicon Valley

PostPosted: Fri May 22, 2009 9:46 am
Reply with quote

Another approach:

Code:
dsn = "AAA.DEEE.E33.R55.T66"
qualifiers = TRANSLATE(dsn,' ','.')

do  i = 1 to words(qualifiers)
    say right(i,2) word(qualifiers, i)
end
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


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

PostPosted: Fri May 22, 2009 3:34 pm
Reply with quote

Enrico,

your solution is much better code than mine. As Pedro pointed out,
I limited myself to 10 nodes.

though the TRANSLATE function is slick, using WORD to separate the parts
inhibits the proofing that the TS required:
Quote:
Actually I need to check if the first letter of every substring is an alphabet and neither number nor spaces. For this I need to seperate PRD, Y00205, REXX, QHTOOL, XXX, YYY.
Back to top
View user's profile Send private message
Pedro

Global Moderator


Joined: 01 Sep 2006
Posts: 2547
Location: Silicon Valley

PostPosted: Fri May 22, 2009 9:27 pm
Reply with quote

The actual proofing has not been discussed yet. It would be something like this:

Code:
/* rexx */                                                             
dsn = "AAA.!EEE.E33.R55.T66"                                           
qualifiers = TRANSLATE(dsn,' ','.')                                     
                                                                       
validC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'                         
do  i = 1 to words(qualifiers)                                         
    say right(i,2) word(qualifiers, i)                                 
    If TRANSLATE(left(word(qualifiers, i),1) ,' ', validC) ¬= ' ' Then 
       Say 'wrong qualifier'                                           
end 
Back to top
View user's profile Send private message
Pedro

Global Moderator


Joined: 01 Sep 2006
Posts: 2547
Location: Silicon Valley

PostPosted: Sat May 23, 2009 1:19 am
Reply with quote

Quote:
It would be something like this:


It would be something like that, but NOT exactly that. As DBZ mentioned, there might be some other error conditions to look for.

And now I hesitate to give entire solutions for fear that some of the forumers will not learn to do on their own.
Back to top
View user's profile Send private message
Terry Heinze

JCL Moderator


Joined: 14 Jul 2008
Posts: 1249
Location: Richfield, MN, USA

PostPosted: Sat May 23, 2009 1:28 am
Reply with quote

Ah yes, you must be referring to the popular saying, "Give a man a fish and he'll eat for a day; teach a man to fish and he'll buy a bass boat, go out to the lake, and drink beer all day." icon_lol.gif
Back to top
View user's profile Send private message
MBabu

Active User


Joined: 03 Aug 2008
Posts: 400
Location: Mumbai

PostPosted: Sat May 23, 2009 9:03 am
Reply with quote

here is the dumbest answer you will see today but good for a laugh I guess.
Code:
/* REXX - assumes running in TSO, English */ 
parse upper arg dsn .                                         
call msg 'OFF'                                                 
if pos('INVALID DATASET NAME',sysdsn("'" || dsn || "'"))>0 then
   say dsn 'is not a valid data set name'
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Sat May 23, 2009 12:24 pm
Reply with quote

follow on to check for alpha only
Code:
dsn = "AAA.DEEE.E33.R55.T66"
do  i = 1 while dsn <> ""
    parse var dsn hlq "." dsn
    hlq.i = hlq
    hlq.0 = i
end

do  i = 1 to hlq.0
    say right(i,2) hlq.i
end

do  i = 1 to hlq.0
    if  datatype(left(hlq.i,1)) <> "M" then ,
        say "invalid qualifier" right(i,2) hlq.i
end


re edited to fix sloppy testing ...
use as Dick suggested
Code:

do  i = 1 to hlq.0
    if  datatype(left(hlq.i,1),"M") <> 1  then ,
        say "invalid qualifier" right(i,2) hlq.i
end
Back to top
View user's profile Send private message
Marso

REXX Moderator


Joined: 13 Mar 2006
Posts: 1353
Location: Israel

PostPosted: Sun May 24, 2009 12:45 am
Reply with quote

Pragati Soni wrote:
I need to count number of '.' in a string like 'PRD.Y00205.REXX.QHTOOL.XXX.YYY'.

Actually I need to check if the first letter of every substring is an alphabet and neither number nor spaces.


If you need to validate a dataset name, there are other ways too:
* If you receive this value from a panel, you can use the VER(...,DSNAME) command,
* You can use SYSDSN to check both validity of name and existence of file.

ISPF uses the following rules for its dataset name validation:
Quote:
A data set name qualifier must begin with an alphabetic character (A-Z, $, @, or #). The remaining characters must be either uppercase alphanumeric or a hyphen (-). A period is used to connect each qualifier in the data set name.

ISPF first determines if the TSO/E NOPREFIX PROFILE option is in use. If it is, ISPF does use a prefix in the calculation of the data set length. A maximum of 44 characters can be entered for a data set name, if that data set name is enclosed in quotes. If the TSO/E NOPREFIX PROFILE option is in use, a maximum of 44 characters can be entered for a data set name when it is not enclosed within quotes. If the TSO/E NOPREFIX PROFILE option is not in use, a maximum of 42 characters can be entered for a data set name, not enclosed in quotes. ISPF uses the minimum data set prefix of two characters (one character and a period separator) during its calculation of the data set name length.
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


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

PostPosted: Sun May 24, 2009 4:09 pm
Reply with quote

spent so much time insuring the bit & bytes were ok.......

ah, the forest for the trees.

thx Marso for injecting a little 'right tool for the job' thinking.
Back to top
View user's profile Send private message
Marso

REXX Moderator


Joined: 13 Mar 2006
Posts: 1353
Location: Israel

PostPosted: Sun May 24, 2009 4:36 pm
Reply with quote

Sometimes I wish I could inject also some 'right question for the job' thinking...
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: Sun May 24, 2009 11:21 pm
Reply with quote

Sometimes we get lucky and can do so. . . icon_smile.gif
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 -> CLIST & REXX

 


Similar Topics
Topic Forum Replies
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts PARSE Syntax for not fix length word ... JCL & VSAM 7
No new posts Sortjoin and Search for a String and ... DFSORT/ICETOOL 1
No new posts Pulling a fixed number of records fro... DB2 2
No new posts Substring number between 2 characters... DFSORT/ICETOOL 2
Search our Forums:

Back to Top