Results 1 to 2 of 2
  1. #1
    2 Star Lounger
    Join Date
    Aug 2014
    Posts
    123
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Modulo inconsistency in batch

    Was writing some code for a maze constructor and used modulus in the batch script (below) and get ERROR: missing operator
    Yet when I run (cut/paste) the code interactively it works.
    I tried several values of val (even enters these lines as 0)
    Is this just me or a bug? Would someone check me and respond?
    Maybe I did something prior which is messing up. The full subroutine is shown below these lines.

    echo BEG even=%even% val=%val%
    set /A even=%val% % 2
    echo FIN even=%even% val=%val%

    :RANDU
    REM Calculate random value for vert/horizontal values
    set /A val=%RANDOM% * (%maxv% - %minv% + 1) / 32768 + %minv%
    REM Calculate an even or odd val even=0 else 1=odd
    set even=0
    echo BEG even=%even% val=%val%
    set /A even=%val% % 2
    echo FIN even=%even% val=%val%
    Thanks

  2. #2
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,413
    Thanks
    33
    Thanked 195 Times in 175 Posts
    That's an easy one - remember you have to double the % signs when in a BATch file.
    Usually that means changing %a to %%a

    Thus, your first SET /A statement should be:
    set /a even=%val% %% 2

    (Of course the variable name should really be something like "remainder" !)
    BATcher

    Time prevents everything happening all at once...

  3. The Following User Says Thank You to BATcher For This Useful Post:

    spark55 (2015-05-26)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •