Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

How to? need help with "function"

Aug
258
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
 
Why are both values true/false evaluated and given back?
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.
 
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?
 
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)
 
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!
 
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?

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.
 
: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
 
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
 

Similar threads

Back
Top