Hi Again!
I was thinking of writing a subroutine that would do different things depending on the name it was called by. Pretty soon, it had a long list of labels before the main body of the subroutine.
I was wondering if it’s possible to have multiple labels on the same line using some twisted trick with embedded newlines or whatever. Maybe something like Jeb’s self-executing label (see seventh entry), but I don’t understand how it works.
Any suggestions appreciated.
- SB
multiple labels on one line
Moderator: DosItHelp
-
- Posts: 216
- Joined: 01 Oct 2012 13:32
- Location: Ireland
- Contact:
Re: multiple labels on one line
Hi Sponge Belly,
The self-executing label is a nice trick but I can't see how it could help here.
It's simply a label and also a line that can be executed.
But another trick could help.
But effectivly it's the same as you use a parameter, but here is the "subFunction" a part of the label name, but as their exists stop characters for the label (like +<>) the name of the label is only ":mySub" but the calling label name is still ":mySub+12" and %0 contains always the complete calling label name.
jeb
Sponge Belly wrote:Maybe something like Jeb’s self-executing label (see seventh entry), but I don’t understand how it works.
The self-executing label is a nice trick but I can't see how it could help here.
It's simply a label and also a line that can be executed.
Code: Select all
@echo off
call :label
echo After the call
<:label <nul echo Executing label line
echo Subfunction
exit /b
But another trick could help.
Code: Select all
@echo off
call :mySub+12
call :mySub+42
exit /b
:mySub
set "subFunc=%~0"
set "subFunc=%subFunc:~7%"
echo SubFunc=%subFunc%
exit /b
Output wrote:SubFunc=12
SubFunc=42
But effectivly it's the same as you use a parameter, but here is the "subFunction" a part of the label name, but as their exists stop characters for the label (like +<>) the name of the label is only ":mySub" but the calling label name is still ":mySub+12" and %0 contains always the complete calling label name.
jeb
-
- Posts: 216
- Joined: 01 Oct 2012 13:32
- Location: Ireland
- Contact:
Re: multiple labels on one line
Hi Jeb!
Thanks for replying. I read about the self-executing labels again. I think it’s starting to sink in. The less than in <:%bl.start% was confusing me. Now I get it. It has to be there to trick the parser into treating the line as a command and not a label, but goto treats labels literally and won’t expand the per cents and will ignore the less than at the start of <:%bl.start%. Fiendishly clever!
And thanks for showing me how to use call :mainfunc+subfunc as a pseudo-parameter. That may come in very handy when I get round to writing my magnus opus. I plan on starting any day now…
- SB
Thanks for replying. I read about the self-executing labels again. I think it’s starting to sink in. The less than in <:%bl.start% was confusing me. Now I get it. It has to be there to trick the parser into treating the line as a command and not a label, but goto treats labels literally and won’t expand the per cents and will ignore the less than at the start of <:%bl.start%. Fiendishly clever!
And thanks for showing me how to use call :mainfunc+subfunc as a pseudo-parameter. That may come in very handy when I get round to writing my magnus opus. I plan on starting any day now…
- SB