DO ... pipe ... simplified

May 20, 2008
10,634
81
Syracuse, NY, USA
This is a drastic simplification of what (I think) is going on it the DO/pipe thread. It uses only internals and Windows's SORT.EXE. Now it seems to have something to do with @EXECARRAY and @EXECSTR but I'm pretty much at a loss and that's just a guess. Using these two BTMs (attached) ...
Code:
v:\simplified> type caller.btm
echo %@word[1,%@execstr[call .\callee.btm | ffind /vkme"average"]]
Code:
v:\simplified> type callee.btm
setarray /f z[5]
echo %@execarray[z,echo 5^r^n4^r^n3^r^n2^r^n1 > tmpfile] > NUL
c:\windows\system32\sort tmpfile
echo average 666
unsetarray z
This is what I get from v25 build 21.
Code:
v:\simplified> callee.btm
1
2
3
4
5
average 666

v:\simplified> caller.btm
<output file>
The pipe is being closed.
ECHO is OFF
In contrast, this (which is expected) is what I get from v25 build 20.
Code:
v:\simplified> callee.btm
1
2
3
4
5
average 666

v:\simplified> caller.btm
666
 

Attachments

May 20, 2008
10,634
81
Syracuse, NY, USA
P.S., It works fine if I don't use @EXECARRAY.
Code:
v:\simplified> type callee.btm
::setarray /f z[5]
::echo %@execarray[z,echo 5^r^n4^r^n3^r^n2^r^n1 > tmpfile] > NUL
echo 5^r^n4^r^n3^r^n2^r^n1 > tmpfile
c:\windows\system32\sort tmpfile
echo average 666
::unsetarray z

v:\simplified> callee.btm
1
2
3
4
5
average 666

v:\simplified> caller.btm
666
 
Apr 13, 2010
298
7
60
The Hague
@execarray catches stdout to the array. Your batch has a command that redirects the result to a file leaving nothing for the array.

You can: (1) use @EXECARRAY and forget the file or (2) write the file and use @FileArray instead?

BTW, ^n is sufficient. You don't need ^r.
 
May 20, 2008
10,634
81
Syracuse, NY, USA
@execarray catches stdout to the array. Your batch has a command that redirects the result to a file leaving nothing for the array.

You can: (1) use @EXECARRAY and forget the file or (2) write the file and use @FileArray instead?

BTW, ^n is sufficient. You don't need ^r.
There are lots of workarounds but it has worked right up to v25 build 21. I figure there was some unintended consequence of a recent change.

As for ^n vs. ^r^n ... oh really! I like proper newlines.

Code:
v:\simplified> echo 5^n4^n3^n2^n1 > tmpfile & list /x tmpfile
1570823556119.png
 
Apr 13, 2010
298
7
60
The Hague
The wording of your reply makes me uncertain whether my feedback to your code was clear.

I was referring to line 2 in your file 'Callee.BTM'
The reason your code does not work is that the redirection of your command's output prevents it to be assigned to the elements of your array. I would be very surprised if that code would work in a previous version. If so, I consider the current version an improval.

Perhaps the following is closer to your preferred coding style:
Code:
echo %@execarray[z,echo 5^r^n4^r^n3^r^n2^r^n1 | tee tmpfile] > NUL
Is there an issue I have not addressed?
 
Last edited:
May 20, 2008
10,634
81
Syracuse, NY, USA
I really don't care about what gets into the array. Notice that the BTM doesn't use the array. It's just the use of @execarray that messes things up. Here are the files again and v25's output folllowed by v24's output. With v25 the error message comes from sort.exe (that's more obvious if I use Gnu sort, which identifies itself).

Code:
v:\simplified> type caller.btm
echo %@word[1,%@execstr[.\callee.btm | ffind /vkme"average"]]

v:\simplified> type callee.btm
setarray /f z[5]
echo %@execarray[z,echo 5^n4^n3^n2^n1 > tmpfile] > NUL
c:\windows\system32\sort tmpfile
del /q tmpfile
echo average 666
unsetarray z

v:\simplified> caller.btm
<output file>
The pipe is being closed.
ECHO is OFF

v:\simplified> d:\tc24\tcc.exe /c caller.btm
666
 
May 20, 2008
10,634
81
Syracuse, NY, USA
Here's one that's less confusing and more like the problem that motivated this discussion. First the two BTMs, then v25's output, then v24's output (again, the error message with v25 comes from SORT.EXE).

Code:
v:\simplified> type caller.btm
echo %@word[1,%@execstr[.\callee.btm | ffind /vkme"average"]]

v:\simplified> type callee.btm
setarray /f z[5]
echo %@execarray[z,echo 5^r^n4^r^n3^r^n2^r^n1] > NUL
echo %z[0] > tmpfile
sort tmpfile
del /q tmpfile
echo average 666

v:\simplified> caller.btm
<output file>
The pipe is being closed.
ECHO is OFF

v:\simplified> d:\tc24\tcc.exe /c caller.btm
666
 
May 20, 2008
10,634
81
Syracuse, NY, USA
Here's what may be another symptom of the same thing. My timecheck.btm outputs this.
Code:
v:\> timecheck.btm
                        LOCAL
HOST                    OFFSET  STRATUM RTT
1.ubuntu.pool.ntp.org   -0.002     2    93
2.us.pool.ntp.org       +0.003     2    92
0.us.pool.ntp.org       +0.004     2    85
0.ubuntu.pool.ntp.org   +0.005     3    88
ntp0.cornell.edu        +0.006     2    22
3.ubuntu.pool.ntp.org   +0.007     2    23
3.us.pool.ntp.org       +0.013     2    75
1.us.pool.ntp.org       +0.017     2    16

8 hosts responding
adjusted average: +0.006 (16:36:13 Sat 2019-10-12)
Each line naming a host comes from a DO loop which, each time through the loop ... uses @EXECARRAY on a timesync (plugin) command, processes the array, and echoes lines to a temp file. When the DO loop is finished, the temp file is sorted (by offset) and sort's output goes to the console. Later, the last two (summary) lines are simply echoed.

When I do this (below), wanting to pick out the last line,

Code:
timecheck.btm  | tpipe /grep=3,0,0,0,0,0,0,0,"average"
sometimes I get

Code:
v:\> timecheck.btm  | tpipe /grep=3,0,0,0,0,0,0,0,"Average"
d:\gnu\sort: fflush failed: standard output: Invalid argument
d:\gnu\sort: write error
and sometimes I get the expected output AFTER THE NEXT PROMPT HAS BEEN ISSUED!!!

Code:
v:\> timecheck.btm  | tpipe /grep=3,0,0,0,0,0,0,0,"Average"

v:\> adjusted average: +0.007 (16:37:19 Sat 2019-10-12)
It works fine with TCC v24.
 
May 20, 2008
10,634
81
Syracuse, NY, USA
Using a much simpler BTM I haven't see any errors from sort, but the output appears after the next prompt has been issued.
Code:
v:\simplified> type callee.btm
setarray /f z[5]
echo %@execarray[z,echo 5^r^n4^r^n3^r^n2^r^n1] > NUL
echo %z[0] > tmpfile
sort tmpfile
del /q tmpfile
echo average 666

v:\simplified> callee.btm | tpipe /grep=3,0,0,0,0,0,0,0,"average"

v:\simplified> average 666
 
May 20, 2008
10,634
81
Syracuse, NY, USA
And with that BTM, piping FFIND instead of TPIPE, I get the (SORT) error every time.

Code:
v:\simplified> type callee.btm
setarray /f z[5]
echo %@execarray[z,echo 5^r^n4^r^n3^r^n2^r^n1] > NUL
echo %z[0] > tmpfile
sort tmpfile
del /q tmpfile
echo average 666

v:\simplified> callee.btm
5
average 666

v:\simplified> callee.btm | ffind /vkme"average"
<output file>
The pipe is being closed.

v:\simplified>
If I use GREP or FINDSTR I get no output at all.

Code:
v:\simplified> callee.btm | findstr average

v:\simplified> callee.btm | grep average

v:\simplified>