Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Counting number of characters in a string

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> CLIST & REXX
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    Post subject: Counting number of characters in a string
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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

Senior Member


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

PostPosted: Thu May 21, 2009 9:41 pm    Post subject: Reply to: Counting number of characters in a string
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

Global Moderator


Joined: 14 Mar 2007
Posts: 10203
Location: italy

PostPosted: Thu May 21, 2009 10:15 pm    Post subject: Reply to: Counting number of characters in a string
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

Senior Member


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

PostPosted: Fri May 22, 2009 9:46 am    Post subject: Reply to: Counting number of characters in a string
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    Post subject:
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

Senior Member


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

PostPosted: Fri May 22, 2009 9:27 pm    Post subject: Reply to: Counting number of characters in a string
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

Senior Member


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

PostPosted: Sat May 23, 2009 1:19 am    Post subject: Reply to: Counting number of characters in a string
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: 1238
Location: Richfield, MN, USA

PostPosted: Sat May 23, 2009 1:28 am    Post subject:
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: 401
Location: Mumbai

PostPosted: Sat May 23, 2009 9:03 am    Post subject: Reply to: Counting number of characters in a string
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

Global Moderator


Joined: 14 Mar 2007
Posts: 10203
Location: italy

PostPosted: Sat May 23, 2009 12:24 pm    Post subject: Reply to: Counting number of characters in a string
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: 1219
Location: Israel

PostPosted: Sun May 24, 2009 12:45 am    Post subject: Re: Counting number of characters in a string
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    Post subject:
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: 1219
Location: Israel

PostPosted: Sun May 24, 2009 4:36 pm    Post subject:
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

Site Director


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

PostPosted: Sun May 24, 2009 11:21 pm    Post subject: Reply to: Counting number of characters in a string
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    IBMMAINFRAMES.com Support Forums -> CLIST & REXX All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
This topic is locked: you cannot edit posts or make replies. Checking wether a string starts from ... sundaram.naveen CLIST & REXX 9 Thu Oct 06, 2016 11:56 am
No new posts Sequence number add in SORT pshongal SYNCSORT 3 Fri Sep 02, 2016 3:32 pm
No new posts Random Password (in string format) ge... ezhavendhan COBOL Programming 10 Mon Aug 29, 2016 3:18 pm
No new posts How to insert a lengthy string havin... vidyaa DB2 7 Thu Aug 25, 2016 5:20 pm
No new posts Which function does the Ampersand (&a... Andi1982 PL/I & Assembler 3 Wed Aug 24, 2016 2:05 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us