1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to? need help with "function"

Discussion in 'Support' started by Frank, Jan 21, 2012.

  1. Frank

    Joined:
    Aug 2, 2011
    Messages:
    258
    Likes Received:
    4
    Hi all,

    I want to format a csv report to a human-readable style.
    The report contains fields for nas-volumes, eg. volume size.
    The size field is notated in KB and I want to replace it with MB, GB, TB if reasonable.

    So in the first step I want to create a function which should return a numeric value divided by 1024 if reasonalble. But it doesn't work as expected. Have a look at this:

    Code:
    C:\Temp >function tomega=%%@if[ %%@eval[ %%1 mod 1024 ] eq 0,%%@eval[%%1/1024] (true),%%1 (false)]
     
    C:\Temp >function
    tomega=%@if[ %@eval[ %1 mod 1024 ] eq 0,%@eval[%1/1024] (true),%1 (false)]
     
    C:\Temp >set val=16384
     
    C:\Temp >echo %@tomega[%val]
    16 (true)
     
    C:\Temp >set val=123456
     
    C:\Temp >echo %@tomega[%val]
    5625 (true),123456 (false)                             <- unexpected
     
    rem    with back quotes    *************************
     
    C:\Temp >function tomega=`%@if[ %@eval[ %1 mod 1024 ] eq 0,%@eval[%1/1024] (true),%1 (false)]`
     
    C:\Temp >function
    tomega=%@if[ %@eval[ %1 mod 1024 ] eq 0,%@eval[%1/1024] (true),%1 (false)]
     
    C:\Temp >set val=16384
     
    C:\Temp >echo %@tomega[%val]
    16 (true)
     
    C:\Temp >set val=123456
     
    C:\Temp >echo %@tomega[%val]
    5625 (true),123456 (false)                             <- unexpected
    
    Why are both values true/false evaluated and given back?

    regards
    Frank
     
  2. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,874
    Likes Received:
    30
    I don't see that:

    Code:
    v:\> function tomega=`%@if[ %@eval[ %1 mod 1024 ] eq 0,%@eval[%1/1024] (true),%1 (false)]`
     
    v:\> function
    tomega=%@if[ %@eval[ %1 mod 1024 ] eq 0,%@eval[%1/1024] (true),%1 (false)]
     
    v:\> set val=16384
     
    v:\> echo %@tomega[%val]
    16 (true)
     
    v:\> set val=123456
     
    v:\> echo %@tomega[%val]
    123456 (false)
    And I can't figure out where 5625 may have come from.
     
  3. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,812
    Likes Received:
    82
    Not reproducible here:

    [D:\jpsoft.com]function tomega=%%@if[ %%@eval[ %%1 mod 1024 ] eq 0,%%@eval[%%1/1024] (true),%%1 (false)]

    [D:\jpsoft.com]function
    tomega=%@if[ %@eval[ %1 mod 1024 ] eq 0,%@eval[%1/1024] (true),%1 (false)]

    [D:\jpsoft.com]set val=16384

    [D:\jpsoft.com]echo %@tomega[%val]
    16 (true)

    [D:\jpsoft.com]set val=123456

    [D:\jpsoft.com\images]echo %@tomega[%val]
    123456 (false)

    Are you sure you don't have an alias or another UDF getting in the way?
     
  4. Frank

    Joined:
    Aug 2, 2011
    Messages:
    258
    Likes Received:
    4
    Hi,

    I still have the problem, but I suppose it is a language-/country-problem.
    Perhaps there is 1 comma to much bugging the function:

    Code:
    C:\Temp >ver /r
     
    TCC  13.01.30 x64  Windows 2008R2 [Version 6.1.7601]
    TCC Build 30  Windows 2008R2 Build 7601  Service Pack 1
    Registered to ********************
     
    C:\Temp >unalias *
     
    C:\Temp >alias
    TCC: No aliases defined
     
    C:\Temp >function tomega=`%@if[ %@eval[ %1 mod 1024 ] eq 0,%@eval[%1/1024] (true),%1 (false)]`
     
    C:\Temp >function
    tomega=%@if[ %@eval[ %1 mod 1024 ] eq 0,%@eval[%1/1024] (true),%1 (false)]
     
    C:\Temp >set val=16384
     
    C:\Temp >echo %@eval[%val/1024]
    16
     
    C:\Temp >echo %@tomega[%val]
    16 (true)
     
    C:\Temp >set val=123456
     
    C:\Temp >echo %@eval[%val/1024]
    120,5625
     
    C:\Temp >echo %@tomega[%val]
    5625 (true),123456 (false)
    
     
  5. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,812
    Likes Received:
    82
    @IF cannot deal with random embedded commas -- that requires the DWIM parser addon.
     
  6. Frank

    Joined:
    Aug 2, 2011
    Messages:
    258
    Likes Received:
    4
    ok, I can follow that "function" is fragile concerning commas.
    But I don't know the addons you mentioned. Isn't there a more common and easy way to accomplish it?

    btw:
    I wish you all a happy new year!
     
  7. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,286
    Likes Received:
    39
    Rex is being facetious: DWIM = "Do What I Mean". Such an addon would require artificial intelligence.
     
  8. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,874
    Likes Received:
    30
    The stray comma and the 5625 are probably coming from @EVAL[ 123456 / 1024 ] (120.5625). You can use @EVAL[ %1 \ 1024 ]. That's the integer quotient and won't create a decimal separator.
     
  9. Frank

    Joined:
    Aug 2, 2011
    Messages:
    258
    Likes Received:
    4
    :oops: thank you for explaining me that DWIM was meant sarcastic :D Unfortunately I'm not so familiar with the american or english language.

    OK, I will find an workaround to avoid the stray comma.

    Thank you.

    regards
    Frank
     
  10. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,812
    Likes Received:
    82
    You can either wrap the @IF function with a SETDOS /G command (to temporarily change your decimal separator), or rewrite your @EVAL statement to use an integer operator, like MOD.
     
  11. Frank

    Joined:
    Aug 2, 2011
    Messages:
    258
    Likes Received:
    4
    Code:
    C:\Temp >function tomega=`%@if[ %@eval[ %1 mod 1024 ] eq 0,%@eval[%1 \ 1024] (true),%1 (false)]`
     
    C:\Temp >function
    tomega=%@if[ %@eval[ %1 mod 1024 ] eq 0,%@eval[%1 \ 1024] (true),%1 (false)]
     
    C:\Temp >set val=16384
     
    C:\Temp >echo %@tomega[%val]
    16 (true)
     
    C:\Temp >set val=123456
     
    C:\Temp >echo %@tomega[%val]
    123456 (false)
    So everything is fine now.

    regards
    Frank
     

Share This Page