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!

a.i.k as argument to a sub routine or procedure

Status
Not open for further replies.

dietmar0609

Programmer
Nov 25, 2008
8
FR
Another simple question. Is it possible to pass a matrix to a subroutine or procedure? And back to the calling routine ...

the a.i.k of the calling pgm should not be modified.
 
Hello dietmar0609,

What REXX-interpreter you use?

In ooREXX you can pass stem as an argument to a procedure using USE ARG and the procedure can return stem. In other REXXes this doesn't work, therefore the universal approach is to use stems as global variables and process them in procedures using EXPOSE.
However I agree, that this is not very elegant: In the school they learned me too, that global variables are very evil, but now from the practice I know, that there are some laguages with only global variables - e.g. COBOL.
So a programmer need to survive without local variables too.

The following example works only in ooREXX (open object REXX):
Code:
[COLOR=#0000ff]/* initialize all stem elements */[/color]
a.[COLOR=#804040][b]=[/b][/color]0


[COLOR=#0000ff]/* Matrix */[/color]
a.0.name [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'A'[/color] [COLOR=#0000ff]/* Name of the Matrix */[/color]
a.0.rows [COLOR=#804040][b]=[/b][/color] 2   [COLOR=#0000ff]/* number of rows */[/color]
a.0.cols [COLOR=#804040][b]=[/b][/color] 3   [COLOR=#0000ff]/* number of columns */[/color] 
[COLOR=#0000ff]/* matrix elements */[/color]
a.1.1 [COLOR=#804040][b]=[/b][/color] 1[COLOR=#804040][b];[/b][/color] a.1.2[COLOR=#804040][b]=[/b][/color]2[COLOR=#804040][b];[/b][/color] a.1.3[COLOR=#804040][b]=[/b][/color]3
a.2.1 [COLOR=#804040][b]=[/b][/color] 3[COLOR=#804040][b];[/b][/color] a.2.2[COLOR=#804040][b]=[/b][/color]2[COLOR=#804040][b];[/b][/color] a.2.3[COLOR=#804040][b]=[/b][/color]1

[COLOR=#0000ff]/* Main program */[/color]
[COLOR=#804040][b]call[/b][/color][COLOR=#008080] print_matrix[/color] a.
[COLOR=#804040][b]say[/b][/color]

e.[COLOR=#804040][b]=[/b][/color][COLOR=#008080]create_1_matrix([/color]3[COLOR=#008080])[/color]
[COLOR=#804040][b]call[/b][/color][COLOR=#008080] print_matrix[/color] e.
[COLOR=#804040][b]say[/b][/color]

t.[COLOR=#804040][b]=[/b][/color][COLOR=#008080]transpose_matrix([/color]a.[COLOR=#008080])[/color]
[COLOR=#804040][b]call[/b][/color][COLOR=#008080] print_matrix[/color] t.
[COLOR=#804040][b]say[/b][/color]

[COLOR=#804040][b]exit[/b][/color]


[COLOR=#0000ff]/* Procedures */[/color]
[COLOR=#008080]print_matrix[/color]: [COLOR=#804040][b]procedure[/b][/color]
  [COLOR=#0000ff]/* in ooREXX stem arguments are possible with USE ARG */[/color]
  [COLOR=#804040][b]use arg[/b][/color] m. [COLOR=#0000ff]/* Matrix */[/color]
  
  name[COLOR=#804040][b]=[/b][/color]m.0.name
  rows[COLOR=#804040][b]=[/b][/color]m.0.rows
  cols[COLOR=#804040][b]=[/b][/color]m.0.cols
  [COLOR=#804040][b]say[/b][/color] [COLOR=#ff00ff]"The Matrix "[/color][COLOR=#804040][b]||[/b][/color]name[COLOR=#804040][b]||[/b][/color][COLOR=#ff00ff]" has dimensions "[/color][COLOR=#804040][b]||[/b][/color]rows[COLOR=#804040][b]||[/b][/color][COLOR=#ff00ff]" x "[/color][COLOR=#804040][b]||[/b][/color]cols[COLOR=#804040][b]||[/b][/color][COLOR=#ff00ff]" :"[/color]
  [COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color]1[COLOR=#804040][b] to [/b][/color]rows
    matrixrow[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]""[/color] 
    [COLOR=#804040][b]do[/b][/color] j[COLOR=#804040][b]=[/b][/color]1[COLOR=#804040][b] to [/b][/color]cols
      [COLOR=#0000ff]/* compose one row of the n-th matrix */[/color] 
      matrixrow [COLOR=#804040][b]=[/b][/color] matrixrow[COLOR=#804040][b]||[/b][/color]name[COLOR=#804040][b]||[/b][/color][COLOR=#ff00ff]"["[/color][COLOR=#804040][b]||[/b][/color]i[COLOR=#804040][b]||[/b][/color][COLOR=#ff00ff]","[/color][COLOR=#804040][b]||[/b][/color]j[COLOR=#ff00ff]"]= "[/color][COLOR=#804040][b]||[/b][/color]m.i.j
      [COLOR=#804040][b]if[/b][/color] j [COLOR=#804040][b]<[/b][/color] cols[COLOR=#804040][b] then[/b][/color]
        matrixrow [COLOR=#804040][b]=[/b][/color] matrixrow[COLOR=#804040][b]||[/b][/color][COLOR=#ff00ff]", "[/color] [COLOR=#0000ff]/* add comma */[/color]
    [COLOR=#804040][b]end[/b][/color]
    [COLOR=#804040][b]say[/b][/color] matrixrow
  [COLOR=#804040][b]end[/b][/color]
  [COLOR=#804040][b]return[/b][/color]

[COLOR=#008080]create_1_matrix[/color]: [COLOR=#804040][b]procedure[/b][/color]
  [COLOR=#804040][b]parse arg[/b][/color] n  

  [COLOR=#0000ff]/* initialize all stem elements */[/color]
  e.[COLOR=#804040][b]=[/b][/color]0
  [COLOR=#0000ff]/* fill values */[/color]
  e.0.name [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'E'[/color][COLOR=#804040][b]||[/b][/color]n [COLOR=#0000ff]/* Name of the Matrix */[/color]
  e.0.rows [COLOR=#804040][b]=[/b][/color] n      [COLOR=#0000ff]/* number of rows */[/color]
  e.0.cols [COLOR=#804040][b]=[/b][/color] n      [COLOR=#0000ff]/* number of columns */[/color] 
  [COLOR=#0000ff]/* matrix elements */[/color]
  [COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color]1[COLOR=#804040][b] to [/b][/color]n
    e.i.i[COLOR=#804040][b]=[/b][/color]1
  [COLOR=#804040][b]end[/b][/color]
  [COLOR=#0000ff]/* In ooREXX a routine can return a stem variable */[/color]
  [COLOR=#804040][b]return[/b][/color] e.

[COLOR=#008080]transpose_matrix[/color]: [COLOR=#804040][b]procedure[/b][/color]
  [COLOR=#0000ff]/* in ooREXX stem arguments are possible with USE ARG */[/color]
  [COLOR=#804040][b]use arg[/b][/color] m. [COLOR=#0000ff]/* Matrix */[/color]

  [COLOR=#0000ff]/* initialize all stem elements */[/color]
  t.[COLOR=#804040][b]=[/b][/color]0
  [COLOR=#0000ff]/* fill values */[/color]
  t.0.name[COLOR=#804040][b]=[/b][/color]m.0.name[COLOR=#804040][b]||[/b][/color][COLOR=#ff00ff]"T"[/color]
  t.0.rows[COLOR=#804040][b]=[/b][/color]m.0.cols
  t.0.cols[COLOR=#804040][b]=[/b][/color]m.0.rows
  [COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color]1[COLOR=#804040][b] to [/b][/color]t.0.rows
    [COLOR=#804040][b]do[/b][/color] j[COLOR=#804040][b]=[/b][/color]1[COLOR=#804040][b] to [/b][/color]t.0.cols
      t.i.j [COLOR=#804040][b]=[/b][/color]m.j.i
    [COLOR=#804040][b]end[/b][/color]
  [COLOR=#804040][b]end[/b][/color]
   [COLOR=#0000ff]/* In ooREXX a routine can return a stem variable */[/color]
  [COLOR=#804040][b]return[/b][/color] t.

The output:
Code:
The Matrix A has dimensions 2 x 3 :
A[1,1]= 1, A[1,2]= 2, A[1,3]= 3
A[2,1]= 3, A[2,2]= 2, A[2,3]= 1

The Matrix E3 has dimensions 3 x 3 :
E3[1,1]= 1, E3[1,2]= 0, E3[1,3]= 0
E3[2,1]= 0, E3[2,2]= 1, E3[2,3]= 0
E3[3,1]= 0, E3[3,2]= 0, E3[3,3]= 1

The Matrix AT has dimensions 3 x 2 :
AT[1,1]= 1, AT[1,2]= 3
AT[2,1]= 2, AT[2,2]= 2
AT[3,1]= 3, AT[3,2]= 1

The procedure print_matrix() takes as an argument stem.
The function create_1_matrix() takes as an argument a number and returns stem.
The last function transpose_matrix takes stem and returns other stem.

I have some experience with 3 REXXes: with ooREXX and Regina under Windows and with REXX/400 - it is REXX on IBM iSeries (aka AS/400). The above example runs only with ooREXX - it would not run with Regina or REXX/400.
 
Thx for your answer. Looks exactly what I need. Actually I do not know what REXX interpreter I am using. I am on REGINA on windows.

If it does not work (i.e. I am not using ooREX) How can I switch to it ??

I'll try out and send you the result asap.

thx again .... Dietmar
 
I just tried the code. It would not work. What do I have to do to switch to ooREXX ???

Regards Dietmar
 
Hi dietmar0609,

You can download ooREXX from here
You need to do nothing special, to switch to ooREXX. You can have both REXXes installed at the same machine and that's right because Regina has some features that ooREXX doesn't have.
I have my ooREXX-programs associated with *.rex files and my Regina-programs associated with *.rexx.
The ooREXX-programs I call with
Code:
rexx foo.rex
and the Regina-programs I call with
Code:
regina foo.rexx
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top