shift inside parentheses (and one side enclosed nested variables)

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

shift inside parentheses (and one side enclosed nested variables)

Post by MigrationUser »

22 May 2013 20:07
npocmaka

this does not work:

Code: Select all

@echo off
for /l %%c in (1=1=5) do (
	echo --%1--
	shift
)
echo --%1--
here's the output:

Code: Select all

C:\>shift_test.bat ich ni san shi go roku
--ich--
--ich--
--ich--
--ich--
--ich--
--roku--
i.e. it looks like something that need delayed expansion (except that delayed expansion does not help).Same for all cases when shift is inside brackets if, just brackets ...

The only that I've found so far that workarounds this is:

Code: Select all

@echo off
for /l %%c in (1=1=5) do (
	call echo --%%1--
	shift
)
echo --%1--
and the output:

Code: Select all

C:\>shift_test.bat ich ni san shi go roku
--ich--
--ni--
--san--
--shi--
--go--
--roku--
it's a little bit slow ...but works.
Any other ideas?

edit:
call %%1 works in similar way as call %%%nested_variable%%% .I have not tested this with for loop yet but I will...

additional ~ accessing arguments without shift:

Code: Select all

setlocal enableDelayedExpansion
set /a counter=0
for /l %%x in (1, 1, 9) do (
 set /a counter=!counter!+1
 call echo %%!counter! 
)
endlocal
Last edited by npocmaka (07 Nov 2013 14:31)

----------------------------

#2 22 May 2013 20:22
npocmaka

And a test with call and one side enclosed variables (can I call them in this way?) :

Code: Select all

@echo off

set nested=Matryoshka
call echo %%%1%%

for  %%N in (nested) do (
	call echo %%%%N%%
)
and the result of my test:

Code: Select all

C:\>test.bat nested
Matryoshka
Matryoshka
or

Code: Select all

@echo off
call call echo %%%%%1

output:

Code: Select all

C:\>test.bat 2 Matryoshka
Matryoshka
:)

Last edited by npocmaka (23 May 2013 09:30)

----------------------------

#3 23 May 2013 14:39
jeb

It's the normal behaviour, as percent variables and parameters are expanded while the block is parsed.

Your second sample could be smaller

Code: Select all

@echo off
call echo %%%1
test.bat 3 shichi hatchi

Output

Code: Select all

hatchi
----------------------------

#5 15 Jul 2013 21:38
npocmaka


'tunneling' is working also with shift (which was expected):

Code: Select all

@echo off

shift & echo %1
goto :eof

    C:\test>test.bat  Eins zwei
    Eins
Last edited by npocmaka (15 Jul 2013 22:45)

----------------------------

#6 15 Jul 2013 22:14
jeb
npocmaka wrote:

2.SHIFT /n is good at math yikes
That would be unexpected!
BUT I can't reproduce it.

And even your sample seems not to be prove it.
The "zwei" is missing, not the "vier".

It seems that the SHIFT takes only the first number after the slash.

Btw. Warum verwendest du deutsche Zahlen wenn du aus Bulgarien kommst?
Why are you using german numbers when you are from bulgaria?

----------------------------

#7 15 Jul 2013 22:42
npocmaka
jeb wrote:

npocmaka wrote:

2.SHIFT /n is good at math yikes

That would be unexpected!
BUT I can't reproduce it.

And even your sample seems not to be prove it.
The "zwei" is missing, not the "vier".

It seems that the SHIFT takes only the first number after the slash.

Btw. Warum verwendest du deutsche Zahlen wenn du aus Bulgarien kommst?
Why are you using german numbers when you are from bulgaria?
My mistake.Every thing after the first number is ignored.But didn't delete my post fast enough.

Last edited by npocmaka (25 Oct 2013 12:41)

----------------------------

#8 15 Jul 2013 23:02
npocmaka
jeb wrote:

Btw. Warum verwendest du deutsche Zahlen wenn du aus Bulgarien kommst?
Why are you using german numbers when you are from bulgaria?
I don't know...For diversity I suppose

Last edited by npocmaka (25 Oct 2013 12:41)
Post Reply