Simple question about "Function" command...

May 24, 2010
Northlake, Il
Please excuse me if this is a set of stupid questions; I'm rather fond of stupid questions at this point in my life. So, here is a "sample" function definition (purely for illustration purpose; it's not the final version of the function that I'm actually trying to write):

Function Sample=`@Eval[@FileSize[%2 %1,b,%3]/@Files[%2 %1]]`
And here's the "results" of invoking said function:

[Z:\]echo %@Sample[*.txt]
@Eval[@FileSize[ *.txt,b,]/@Files[ *.txt]]

[Z:\]echo %@Sample[*.txt,/S]
@Eval[@FileSize[/S *.txt,b,]/@Files[/S *.txt]]

[Z:\]echo %@Sample[*.txt,/S,a]
@Eval[@FileSize[/S *.txt,b,a]/@Files[/S *.txt]]

[Z:\]echo %@Sample[*.txt,,a]
@Eval[@FileSize[ *.txt,b,a]/@Files[ *.txt]]

Exactly what I would hope for.

So here's function definition in the "attempt" to make the function more "real":

Function Sample=`@Eval[%@FileSize[%2 %1,b,%3]/%@Files[%2 %1]]`

And here's the "results" of trying to invoking the above function:

[Z:\]echo %@Sample[*.txt,/S]

[Z:\]echo %@Sample[*.txt]

[Z:\]echo %@Sample[*.txt,/S]

[Z:\]echo %@Sample[*.txt,/S,a]

[Z:\]echo %@Sample[*.txt,,a]

[Z:\]echo %@Sample[*.txt,``,a]

Not exactly what I would expect. And, more importantly, incorrect!!!

So, the questions are, of course, what is going on here and what am I doing wrong and how do I fix it?
It looks to me like you have no *.TXT files in the current directory. @FILESIZE therefore returns -1, and @FILES returns 0.

Doing this correctly would be pretty tricky in a user-defined function. You would need to check for the no-matching-files condition before doing the division.
Charles, it was a good guess, but it is, simply put, flat-out not the case.


[Z:\]Echo %@Files[*.txt]

And, just for the record:

[Z:\]echo %@Files[*]

(I forgot to mention getting very similar results with the "all" pattern ("*" of course)).

And I'll note there is only a problem when the second argument is not present and the third one argument is, i.e., "Echo %@Sample[*.txt,,]" works fine yielding "@Eval[21546573/185]", whereas "Echo %@Sample[*.txt,,a]" yeilds "@Eval[-1/0]" (which was clearly shown in the second sequence above that ends in "echo %@Sample[*.txt,``,a]" - one of the "failures").

- Dan

But thank you.

- Dan
Okay, I actually tried it. I get different results from you in the first test:

C:\Bin\Words>Function Sample=`@Eval[@FileSize[%2 %1,b,%3]/@Files[%2 %1]]`

C:\Bin\Words>echo %@sample[*.txt,,a]
@Eval[@FileSize[a *.txt,b,]/@Files[a *.txt]]



It seems that, for me at least, TCC is completely ignoring the second, empty, argument, and assigning the third argument to %2.
Charles, what you get is what I expected to get (my dim recollection is that TCC functions do not "accept" null arguments), but, as you can see, it isn't what I did get. However, after thinking about it for a while and "reworking" the function a bit to use "%@UnQuote[%2]" instead of just "%2", and supplying an otherwise empty double-quoted string as the second argument (i.e., %@Sample[*.txt,"",a]) everything now works fine, and it's only slightly inconvenient (if I remember it, of course!!! ; > ) >.

But thank you again.

- Dan

Similar threads