Find a free work file number and use it

Hallo all!

Sometimes, I use a WRITE WORK FILE for debugging purposes. In that case I don’t care about work file numbers - I just have to take a free one. The question is: Is there a way to find a free work file number and to use that number?

Answer: It’s easy to find a free work file number, but it’s complicated to use it.

I wrote the following (for NAT 6.1.1 @ Solaris):

define data local
1 #usr2011
  2 WORK-NUM        (I01)
  2 WORK-FILE       (A253) (EM=X(10))
  2 WORK-TYPE       (A01)
  2 MAX-NUM         (I01)
  2 RESPONSE        (I04)
  2 EXTENDED-DATA   (A01) init <H'00'>
1 #my-workfile (I01)
end-define
*
for #usr2011.WORK-NUM = 1 to 32  /* searching for a free work file number
  CALLNAT 'USR2011N' 'G' #usr2011
* display #usr2011
  if #usr2011.work-file = " " and #usr2011.response = 0
    #usr2011.work-type := "U"          /* Unformatted
    #usr2011.work-file := "/tmp/test"
    CALLNAT 'USR2011N' 'S' #usr2011    /* Set
    if #usr2011.response = 0
      #my-workfile := #usr2011.work-num
    end-if
    escape bottom
  end-if
end-for
*
if #my-workfile = 0
  write 'no free work file number found!"
else
* write work file #my-workfile 'testvar'   /* would throw NAT0076 here
  decide on first value of #my-workfile
    value 01 write work file 01 'test01'
    value 02 write work file 02 'test02'
    value 03 write work file 03 'test03'
    value 04 write work file 04 'test04'
    value 05 write work file 05 'test05'
    value 06 write work file 06 'test06'
    value 07 write work file 07 'test07'
    value 08 write work file 08 'test08'
    value 09 write work file 09 'test09'
    value 10 write work file 10 'test10'
    value 11 write work file 11 'test11'
    value 12 write work file 12 'test12'
    value 13 write work file 13 'test13'
    value 14 write work file 14 'test14'
    value 15 write work file 15 'test15'
    value 16 write work file 16 'test16'
    value 17 write work file 17 'test17'
    value 18 write work file 18 'test18'
    value 19 write work file 19 'test19'
    value 20 write work file 20 'test20'
    value 21 write work file 21 'test21'
    value 22 write work file 22 'test22'
    value 23 write work file 23 'test23'
    value 24 write work file 24 'test24'
    value 25 write work file 25 'test25'
    value 26 write work file 26 'test26'
    value 27 write work file 27 'test27'
    value 28 write work file 28 'test28'
    value 29 write work file 29 'test29'
    value 30 write work file 30 'test30'
    value 31 write work file 31 'test31'
    value 32 write work file 32 'test32'
    none value ignore
  end-decide
*
* close work file #my-workfile     /* NAT0072 again
  reset #usr2011.work-file
  callnat 'USR2011N' 'S' #usr2011  /* close work file and release number
end-if
*
end

I think this code is unbeautiful. Is there a better way?

Thanks!

Matthias