multiple labels on one line

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Sponge Belly
Posts: 216
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

multiple labels on one line

#1 Post by Sponge Belly » 02 Apr 2013 13:35

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

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: multiple labels on one line

#2 Post by jeb » 02 Apr 2013 16:28

Hi Sponge Belly,

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

Sponge Belly
Posts: 216
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: multiple labels on one line

#3 Post by Sponge Belly » 03 Apr 2013 03:48

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! :twisted:

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

Post Reply