Simple question about "Function" command...

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
#1
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]
@Eval[21547243/186]

[Z:\]echo %@Sample[*.txt]
@Eval[21546573/185]

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

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

[Z:\]echo %@Sample[*.txt,,a]
@Eval[-1/0]

[Z:\]echo %@Sample[*.txt,``,a]
@Eval[-1/0]

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?
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,376
39
Albuquerque, NM
prospero.unm.edu
#2
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.
 
#3
Charles, it was a good guess, but it is, simply put, flat-out not the case.

Specifically:

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

And, just for the record:

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

(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
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,376
39
Albuquerque, NM
prospero.unm.edu
#4
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]]

C:\Bin\Words>option

C:\Bin\Words>


It seems that, for me at least, TCC is completely ignoring the second, empty, argument, and assigning the third argument to %2.
 
#5
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