119) How can I remove the leading zeros a non-negative integer?
@echo off & setlocal enableextensions
set s_= 007
echo %s_%
echo %s_%|findstr [123456789]>nul
if %errorlevel% EQU 0 (
for /f "tokens=* delims=0 " %%a in ('echo %s_%') do (
set s_=%%a)
) else (set s_=0)
echo %s_%
endlocal & goto :EOF
The output will be
C:\_D\TEST>cmdfaq
007
7
Or, using a subroutine
@echo off & setlocal enableextensions
set s_=000
echo %s_%
call :TrimZeros %s_% s_
echo %s_%
endlocal & goto :EOF
::
:: =============================================
:TrimZeros Dymmy1 Return_
setlocal enableextensions
echo %1|findstr [123456789]>nul
if %errorlevel% EQU 0 (
for /f "tokens=* delims=0 " %%a in ('echo %1') do (
set Return_=%%a)
) else (set Return_=0)
endlocal & set "%2=%Return_%" & goto :EOF
The output will be
C:\_D\BAS>cmdfaq
000
0
All the best, Timo
--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip
What's wrong with the obvious (it's immaterial in this case that
SET /a will treat the argument as Octal):
Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
SET N=007
SET /a N+=0
ECHO. N=%N%
====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects
============Screen capture Windows 2000 simulated in Win95
C:\WORK>demo
N=7
C:\WORK>
============End screen capture
--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
From email address not checked. Contact us at http://www.allenware.com/
>"Timo Salmi" wrote in message
>> DRAFT
>>
>> 119) How can I remove the leading zeros a non-negative integer?
>...snip
>
>What's wrong with the obvious (it's immaterial in this case that
>SET /a will treat the argument as Octal):
>
>Lines that don't begin with two spaces have wrapped accidentally
>====Begin cut-and-paste (omit this line)
> @ECHO OFF
> SET N=007
> SET /a N+=0
> ECHO. N=%N%
>
>====End cut-and-paste (omit this line)
Screen dump
----------
d:\MyFiles>SET N=009
d:\MyFiles>SET /a N+=0
d:\MyFiles>ECHO. N=0
N=0
d:\MyFiles>
----------
--
T.E.D. (tda...@gearbox.maem.umr.edu)
SPAM filter: Messages to this address *must* contain "T.E.D."
somewhere in the body or they will be automatically rejected.
<G> Thanks, Ted. Good point, so I guess it has to be the
slightly-less obvious:
Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
SET N=009
SET /a N=1%N%-(11%N%-1%N%)/10
ECHO N=%N%
====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects
============Screen capture Windows 2000 simulated in Win95
C:\WORK>demo
N=9
> What's wrong with the obvious (it's immaterial in this case that
> SET /a will treat the argument as Octal):
Just one trivial example
@echo off & setlocal enableextensions
set s_=008
set /a s_=%s_%+2
endlocal & goto :EOF
The output will be
C:\_D\TEST>cmdfaq
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).
Already dealt with in my earlier reply to Ted with:
Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
SET N=009
SET /a N=1%N%-(11%N%-1%N%)/10
ECHO N=%N%
====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects
--
William, I am sligthly baffled why a FAQ about removing leading
zeros (or some similar lead) should have a special justification (if
you pardon the pun). It always is good, though, to have alternative
methods. Thank you. I will be pleased to add your alternative.
Netvethless, let's take a look at
> @ECHO OFF
> SET N=009
> SET /a N=1%N%-(11%N%-1%N%)/10
> ECHO N=%N%
Take another number:
@echo off & setlocal enableextensions
goto _2
SET N=009111111
SET /a N=1%N%-(11%N%-1%N%)/10
ECHO N=%N%
endlocal & goto :EOF
C:\_D\TEST>cmdfaq
Invalid number. Numbers are limited to 32-bits of precision.
N=009111111
which obviously is not the conversion result we want.
However:
@echo off & setlocal enableextensions
set s_=009111111
echo %s_%
echo %s_%|findstr [123456789]>nul
if %errorlevel% EQU 0 (
for /f "tokens=* delims=0 " %%a in ('echo %s_%') do (
set s_=%%a)
) else (set s_=0)
echo %s_%
endlocal & goto :EOF
C:\_D\TEST>cmdfaq
009111111
9111111
William, I am sligthly baffled why a FAQ about removing leading
zeros (or some similar lead) should have a special justification (if
you pardon the pun). It always is good, though, to have alternative
methods. Thank you. I will be pleased to add your alternative.
Nevertheless, let's take a look at
> @ECHO OFF
> SET N=009
> SET /a N=1%N%-(11%N%-1%N%)/10
> ECHO N=%N%
Take another number:
@ECHO OFF
SET N=009111111
SET /a N=1%N%-(11%N%-1%N%)/10
ECHO N=%N%
C:\_D\TEST>cmdfaq
Invalid number. Numbers are limited to 32-bits of precision.
N=009111111
which obviously is not the result we want.
However:
@echo off & setlocal enableextensions
set s_=009111111
echo %s_%
echo %s_%|findstr [123456789]>nul
if %errorlevel% EQU 0 (
for /f "tokens=* delims=0 " %%a in ('echo %s_%') do (
set s_=%%a)
) else (set s_=0)
echo %s_%
endlocal & goto :EOF
C:\_D\TEST>cmdfaq
009111111
9111111
All the best, Timo
> However:
> @echo off & setlocal enableextensions
> set s_=009111111
> echo %s_%
> echo %s_%|findstr [123456789]>nul
> if %errorlevel% EQU 0 (
> for /f "tokens=* delims=0 " %%a in ('echo %s_%') do (
> set s_=%%a)
> ) else (set s_=0)
> echo %s_%
> endlocal & goto :EOF
>
> C:\_D\TEST>cmdfaq
> 009111111
> 9111111
Alternately:
@echo off & setlocal enableextensions
set s_=009111111
echo %s_%
:loop
set s_=%s_:*0=%
if "%s_:~0,1%"=="0" goto loop
Yes, but if
set s_=000
then
C:\_D\TEST>cmdfaq
000
ECHO is off.
> foxidrive <mi...@melbpc.org.au.gotcha.invalid> wrote:
>> On Wed, 28 Sep 2005 08:00:44 +0000 (UTC), Timo Salmi wrote:
>> Alternately:
>> @echo off & setlocal enableextensions
>> set s_=009111111
>> echo %s_%
>> :loop
>> set s_=%s_:*0=%
>> if "%s_:~0,1%"=="0" goto loop
>> echo %s_%
>> endlocal & goto :EOF
>
> Yes, but if
> set s_=000
> then
> C:\_D\TEST>cmdfaq
> 000
> ECHO is off.
>
> All the best, Timo
Now that's being picky - it removed all the leading zeros! <grin>
So add one line
if "%s_%"=="" set s_=0
Or I suppose, the simpler:
Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
SET N=009
SET /a N=1%N%-2%N%+1%N%
ECHO. N=%N%
====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects
============Screen capture Windows 2000 simulated in Win95
C:\WORK>demo
N=9
C:\WORK>
============End screen capture
and assorted variations on similar algebraic themes.
:: s_=non-negative integer; 009, 000, 0987, whatever...
set s_=009
:loop
if not %s_%==0 if %s_:~0,1%==0 set s_=%s_:~1%&goto loop
set s_
HTH
...Bill
ISTM that +5 and --7 might be non-negative integers; perhaps "unsigned
integer" might be better?
ISTM that, at least by analogy with other computer languages, an integer
should be a binary construct, and does not have optional leading zeroes.
A number with leading zeroes must be a string.
ISTM that "the leading zeroes" implies that there is at least one, "any
leading zeroes" implies that there certainly may be none, and "leading
zeroes" leaves the point open.
119) How can I remove any leading zeros from an unsigned integer string?
The following seems to do it, and I suppose SED could be used likewise :
echo 00500 | mtr -o -x+ - ^(0*)(\d+) = \2
echo 00500 | mtr -o -x+ - ^(0*)(\d+)(.*) = \2
The second removes trailing material too.
However, those methods expect the leading character to be a digit.
echo 00500 | mtr -o -x+ - ^(\D*)(0*)(\d+)(.*) = \3
should extract the first number from a string - well, except for such as
ONE..2X where it gets the second number. There needs to be a digit
present, or the output line is lost; one needs to consider whether a
trailing CRLF is important.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt;
free, DOS/Win/UNIX, <URL:http://www.idiotsdelight.net/minitrue/>
> 119) How can I remove any leading zeros from an unsigned integer string?
Much better, thanks. I am going to use
119} How can I remove any leading zeros from an unsigned integer?
(The "string" is implied enough)