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

Searching a table using REXX


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

New User


Joined: 29 Apr 2008
Posts: 20
Location: United Kingdom

PostPosted: Mon Jun 30, 2008 2:06 pm
Reply with quote

Hi All,

I want to write a REXX which searches a table for a particular value in one column and returns a value in that row. The table will look something like this -

5011202 3
5416789 2

And so on....

So if we give 5011202, it should return 3 as output.

Also how can I make this REXX available globally to all TSO Users ?

Amit.
Back to top
View user's profile Send private message
expat

Global Moderator


Joined: 14 Mar 2007
Posts: 8797
Location: Welsh Wales

PostPosted: Mon Jun 30, 2008 2:13 pm
Reply with quote

Is the table indexed on the variable name that you wish to search ?
If so, look at TBSCAN

To share the REXX, place it in a SYSEXEC / SYSPROC defined library common to all users.
Back to top
View user's profile Send private message
purusothaman

New User


Joined: 17 Feb 2007
Posts: 39
Location: Chennai

PostPosted: Mon Jun 30, 2008 3:25 pm
Reply with quote

Hi,

Try with this and let me know if it works for you.

Code:
/* CONNECT TO DB2 SUB SYSTEM */                             
ADDRESS DSNREXX 'CONNECT YYYY'                               
ADDRESS TSO "STEPLIB DA('YYYY.SDSNEXIT','YYYY.SDSNLOAD') SHR"
ADDRESS SQL                                                 
ADDRESS TSO "SUBCOM DSNREXX"                                 
IF RC THEN                                                   
DO                                                           
   S_RC = RXSUBCOM('ADD','DSNREXX','DSNREXX')               
END                       
/* SELECT QUERY TO GET THE DETAILS */                   
S1 = "SELECT ID FROM YYYY.TABLE_NAME",   
     " WHERE NAME   = JAMES ",                 
     " FOR FETCH ONLY WITH UR"                           
SQLSTMT = S1                                             
                                                         
ADDRESS  DSNREXX "EXECSQL DECLARE C1 CURSOR FOR S1"     
ADDRESS  DSNREXX "EXECSQL PREPARE S1 FROM :SQLSTMT"     
ADDRESS  DSNREXX "EXECSQL OPEN C1"                       
ADDRESS  DSNREXX "EXECSQL FETCH C1 INTO :PKEY "   
SAY PKEY
ADDRESS DSNREXX 'DISCONNECT'         


YYYY - DB2 system to be connected
Back to top
View user's profile Send private message
Manas Sinha

New User


Joined: 29 Apr 2008
Posts: 20
Location: United Kingdom

PostPosted: Mon Jun 30, 2008 3:43 pm
Reply with quote

Apologies for not clarifying this earlier.

The I/P table would be a sequential (PS) file that I would create.

Best regards,
Amit.
Back to top
View user's profile Send private message
expat

Global Moderator


Joined: 14 Mar 2007
Posts: 8797
Location: Welsh Wales

PostPosted: Mon Jun 30, 2008 4:00 pm
Reply with quote

Manas Sinha wrote:
The I/P table would be a sequential (PS) file that I would create.

Please clarify, a PS file or an ISPF table built from the PS file ?

Because if it is a straight PS file, you will need to read it sequentially.
Back to top
View user's profile Send private message
Manas Sinha

New User


Joined: 29 Apr 2008
Posts: 20
Location: United Kingdom

PostPosted: Mon Jun 30, 2008 4:25 pm
Reply with quote

It would be a PS file and since it would be a small file, sequential read will not be a problem.

Still I would be interested in knowing if there is a better way to do it ?

Best regards,
Amit.
Back to top
View user's profile Send private message
ofer71

Global Moderator


Joined: 27 Dec 2005
Posts: 2358
Location: Israel

PostPosted: Mon Jun 30, 2008 7:32 pm
Reply with quote

You can use a binary search (make sure the data is sorted), like in the following example:
Code:
/****************************** REXX ******************************** */
/*                                                                    */
/* Name.......: BINISRCH                                              */
/*                                                                    */
/* Function...: Itterative binary search.                             */
/*                                                                    */
/* Date.......: 13/12/2004.                                           */
/*                                                                    */
/* Author.....: OFER                                                  */
/*                                                                    */
/* Reqirements: -                                                     */
/*                                                                    */
/* Description: Search a sorted array by repeatedly dividing the      */
/*              search interval in half.                              */
/*              Complexity: 0(log n).                                 */
/*                                                                    */
/*              INPUT: Supply your array in DATA.                     */
/*                     Make sure that DATA.0 contains the number of   */
/*                     elements in the array (filled automatically by */
/*                     EXECIO).                                       */
/*                                                                    */
/**********************************************************************/
                                                                       
ADDRESS TSO "ALLOC FI(SAMPDATA) DA('your.input.dataset') SHR"           
            "EXECIO * DISKR SAMPDATA (STEM DATA. FINIS"                 
            "FREE FI(SAMPDATA)"                                         
                                                                       
SAY BIN_SEARCH('your_target')                                           
                                                                       
RETURN                                                                 
                                                                       
/*------------------------------------------------------------------*/ 
  BIN_SEARCH:                                                           
/*------------------------------------------------------------------*/ 
                                                                       
ARG TARGET                                                             
                                                                       
TOP   = DATA.0                                                         
BOT   = 1                                       
FOUND = 0                                       
                                               
DO WHILE (FOUND = 0 & TOP >= BOT)               
                                               
  MID = (TOP + BOT) % 2                         
                                               
  IF TARGET = DATA.MID THEN                     
    FOUND = 1                                   
  ELSE                                         
    IF TARGET < DATA.MID THEN                   
       TOP = MID - 1                           
    ELSE                                       
       BOT = MID + 1                           
END                                             
                                               
RETURN MID                                     


O.
Back to top
View user's profile Send private message
Marso

REXX Moderator


Joined: 13 Mar 2006
Posts: 1353
Location: Israel

PostPosted: Mon Jun 30, 2008 8:54 pm
Reply with quote

If the (same) values have to be searched many times,
it is worth investing in loading a stem table the following way:

Code:
"EXECIO * DISKR TBCONV (FINIS TB1."
After this we have TB1.1 = "5011202 3", TB1.2 = "5416789 2" and so on.

Code:
TB2. = ""
Do Ix = 1 To TB1.0
   Parse Var TB1.Ix F1 F2
   TB2.F1 = F2
End
After this, we have a new stem table with TB2.5011202 = 3, TB2.5416789 = 2 and so on.

Now, replacing one value with the other is easy, no search needed:
Code:
NewCode = TB2.OldCode

NB: it also works with characters, not only with numbers
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 Compile Several JCL JOB Through one r... CLIST & REXX 4
No new posts Running REXX through JOB CLIST & REXX 13
No new posts Error to read log with rexx CLIST & REXX 11
No new posts Load new table with Old unload - DB2 DB2 6
No new posts isfline didnt work in rexx at z/OS ve... CLIST & REXX 7
Search our Forums:

Back to Top