home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / randomz4.zip / Randomizer.cmd < prev    next >
OS/2 REXX Batch file  |  2001-07-29  |  4KB  |  198 lines

  1. /*
  2.     Randomizer 4.0
  3.     (c) 2001 by Don Eitner
  4.  
  5.     Selects random items from a user-defined set of fields.
  6.  
  7.     This code is neither supported nor under warranty.  Feel free to
  8.     examine and modify this script for your own purposes.  See the
  9.     included readme.txt for additional information.
  10. */
  11.  
  12. parse arg CmdLine
  13. parse var CmdLine CL1 CmdLine
  14. CL1 = translate(CL1)
  15.  
  16. call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  17. call SysLoadFuncs
  18.  
  19. call SysCls
  20.  
  21. DataFile = ''
  22. delim = '09'x
  23. delim2 = ','
  24.  
  25. call ParseInput
  26.  
  27. call SysCls
  28.  
  29. Fields = linein(DataFile,,1)
  30. Count = puns(Fields, delim)
  31.  
  32. RC = SysFileDelete("data.out")
  33.  
  34. do Num = 1 to Count
  35.     Item.Num = linein(DataFile,,1)
  36.     Field.Num = pun(Fields, delim, Num)
  37.  
  38.     if Pos('',Field.Num) = 1 then
  39.         do
  40.             Count2 = random(pun(Field.Num, delim2, 2), pun(Field.Num, delim2, 3))
  41.             Field.Num = substr(Field.Num,2)
  42.         end
  43.     else
  44.         Count2 = 1
  45.  
  46.     do Rands = 1 to Count2
  47.         Rand = random(1, puns(Item.Num, delim))
  48.         Data.Num = pun(Item.Num, delim, Rand)
  49.         Item.Num = delpun(Item.Num, delim, Rand, 1)
  50.         say pun(Field.Num, delim2, 1) ':' pun(Data.Num, delim2, 1)
  51.         call lineout "data.out", pun(Field.Num, delim2, 1) ':' pun(Data.Num, delim2, 1)
  52.     end
  53.  
  54. end
  55.  
  56. call lineout("data.out")
  57. call lineout(DataFile)
  58.  
  59. exit
  60.  
  61.  
  62. ParseInput:
  63.  
  64. Again:
  65.  
  66.     if (CL1 = '/Y') then
  67.     do
  68.         if DataInput = 'N' then
  69.             signal GiveHelp
  70.         DataInput = 'Y'
  71.         parse var CmdLine CL1 CmdLine
  72.         CL1 = translate(CL1)
  73.         signal Again
  74.     end
  75.  
  76.     if (CL1 = '/N') then
  77.     do
  78.         if DataInput = 'Y' then
  79.             signal GiveHelp
  80.         DataInput = 'N'
  81.         parse var CmdLine CL1 CmdLine
  82.         CL1 = translate(CL1)
  83.         signal Again
  84.     end
  85.  
  86.     if (CL1 = '/D') then
  87.     do
  88.         parse var CmdLine CL1 CmdLine
  89.         DataFile = CL1
  90.         parse var CmdLine CL1 CmdLine
  91.         CL1 = translate(CL1)
  92.         signal Again
  93.     end
  94.  
  95.     if (CL1 = '/?') then
  96.     do
  97.         signal GiveHelp
  98.     end
  99.  
  100.     if DataFile = '' then
  101.         DataFile = 'RandomData.txt'
  102.  
  103.     select
  104.         when DataInput = 'N' then
  105.             return
  106.         when DataInput = 'Y' then
  107.             call DataInput(DataFile)
  108.         otherwise
  109.         do
  110.             do until (datatype(Junk) = 'CHAR') & (Junk = 'Y' | Junk = 'N')
  111.                 say 'Would you like to edit the list of fields & items (Y/N)?'
  112.                 Junk = translate(SysGetKey('NOECHO'))
  113.             end
  114.             if (Junk = 'Y') then
  115.             call DataInput(DataFile)
  116.         end
  117.     end
  118.  
  119. return
  120.  
  121.  
  122. GiveHelp:
  123.  
  124.     say 'Randomizer v4.0, 2001 by Don Eitner'
  125.     say 'Universal random item selector.'
  126.     say ''
  127.     say ' Usage:  Randomizer [[/Y | /N] [/D DataFile]] [/?]'
  128.     say ''
  129.     say '  Only one or the other of /Y or /N may be given, or neither.'
  130.     say '  If neither is given, program will prompt for input mode.'
  131.     say ''
  132.     say '  /Y = sets input mode on so you can add fields and items'
  133.     say '       to selected data file (datainput.cmd)'
  134.     say '  /N = sets input mode off so you just get random selection'
  135.     say '       from selected data file'
  136.     say '  /D = allows you to choose the name of the data file to use'
  137.     say '  /? = shows this help screen'
  138.  
  139. exit
  140.  
  141.  
  142. puns: procedure
  143.  
  144.     PARSE ARG string, delim
  145.     CALL split string, delim
  146.  
  147. RETURN substring.0
  148.  
  149.  
  150. pun: procedure
  151.  
  152.     PARSE ARG string, delim, word
  153.     CALL split string, delim
  154.  
  155. RETURN substring.word
  156.  
  157.  
  158. delpun: procedure
  159.  
  160.     PARSE ARG string, delim, word, length
  161.     CALL split string, delim
  162.     DO x = word to (word - 1 + length)
  163.         substring.x = ''
  164.     END
  165.     DO y = 1 to substring.0
  166.         IF substring.y = '' THEN
  167.             iterate
  168.         IF string = '' THEN
  169.             string = substring.y
  170.         ELSE
  171.             string = string||delim||substring.y
  172.     END
  173.  
  174. RETURN string
  175.  
  176.  
  177. split:
  178.  
  179.     PARSE ARG string, delim
  180.     num = 0
  181.     DO WHILE string <> ''
  182.         num = num + 1
  183.         stop = pos(delim, string)
  184.         IF stop = 0 THEN
  185.             stop = length(string)+1
  186.         substring.num = substr(string, 1, stop-1)
  187.         string = substr(string, stop+1)
  188.     END
  189.     substring.0 = num
  190.  
  191.     IF delim = ',' | delim = ';' | delim = ':' THEN
  192.         DO x = 1 to substring.0
  193.             substring.x = strip(substring.x,b,'"')
  194.         END
  195.  
  196. RETURN
  197.  
  198.