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
 

 

Searching a table using REXX

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> CLIST & REXX
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    Post subject: Searching a table using REXX
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: 8593
Location: Back in jolly old England

PostPosted: Mon Jun 30, 2008 2:13 pm    Post subject:
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    Post subject:
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    Post subject:
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: 8593
Location: Back in jolly old England

PostPosted: Mon Jun 30, 2008 4:00 pm    Post subject:
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    Post subject:
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: 2360
Location: Israel

PostPosted: Mon Jun 30, 2008 7:32 pm    Post subject:
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: 1224
Location: Israel

PostPosted: Mon Jun 30, 2008 8:54 pm    Post subject:
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    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. Calling REXX code from ISPF panel sanchita dey TSO/ISPF 4 Mon Jan 02, 2017 5:13 pm
No new posts REXX DB2: Dynamic allocation of DB2.D... BHAS CLIST & REXX 3 Mon Dec 19, 2016 8:26 pm
No new posts find particular member name in PDS us... ravi243 CLIST & REXX 10 Mon Dec 19, 2016 6:44 pm
No new posts SQL query to run through list of valu... Ni3-db2 DB2 14 Wed Dec 14, 2016 9:52 am
No new posts What is the command to check MODE of ... rohanthengal CLIST & REXX 7 Fri Nov 18, 2016 1:48 pm


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