Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

An Interesting String length Routine

94 views
Skip to first unread message

Frank P. Westlake

unread,
Nov 11, 2009, 3:05:47 PM11/11/09
to
Here is an interesting string length routine. The interesting thing about it is what values are manipulated to determine the length. This version doesn't handle special characters in the string so this isn't a good general solution, I just thought it was interesting how the math worked out.


:: BEGIN SCRIPT :::::::::::::::::::::::::::::::::::::::::::::
@Echo OFF
SetLocal ENABLEEXTENSIONS
Set "String=%*"
Call :Strlen String
Set /A "StrLen=:StrLen"
Echo.Your string contains %StrLen% characters.
Goto :EOF

:strlen
Set "%0="
For /F "tokens=1 delims=:" %%n in (
'(Echo.%~1^&Set %~1^&echo.^)^|FindStr /o "^"'
) Do Set /a %0=%0*-2+%%n
Goto :EOF

:: END SCRIPT :::::::::::::::::::::::::::::::::::::::::::::::

The string is read from the commandline and stored in a variable. The variable name is then given as the parameter to the :StrLen subroutine.

The subroutine uses its own name as the return variable. This name begins with a colon so it cannot be used as thus: %:StrLen% or !:StrLen!. The colon terminates the name when used that way. It can be printed from 'SET :StrLen' but since the return is a numeric value we can use SET/A to make use of the value or reassign it regardless of the colon. But this isn't the interesting part, it's only an explanation of what might appear confusing.

Using character offset values from FindStr, SET/A calculates the series a=-2a+n, with 'n' being the character offsets of the three lines fed to FindStr. SET/A assigns the value 0 to undefined variables but the statement 'Set "%0="' could also assign the value 0.

Given the string:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

the three offsets are:

0 Beginning of first line.
9 Length of variable name ("StrLen"), space, and line ending (<CR><LF>).
44 The previous 9 plus name, equal sign, contents, and line ending.

CMD.EXE appends a space to the end of the 'Echo.%~1' statement, perhaps to seperate it from the ampersand, but does not append a space to the 'Set %~1' statement. This one extra character is what makes the series work.

Frank

Tom Lavedas

unread,
Nov 11, 2009, 4:02:13 PM11/11/09
to
On Nov 11, 3:05 pm, "Frank P. Westlake" <frank.westl...@yahoo.com>
wrote:

See: http://groups.google.com/group/alt.msdos.batch.nt/browse_frm/thread/f5aa5405d3252bd6/563e204b81f2b229
_____________________
Tom Lavedas

Frank P. Westlake

unread,
Nov 11, 2009, 5:11:03 PM11/11/09
to
"Tom Lavedas" news:a97717b3-7a39-44d4...@15g2000yqy.googlegroups.com...


See: http://groups.google.com/group/alt.msdos.batch.nt/browse_frm/thread/f5aa5405d3252bd6/563e204b81f2b229

I'm not sure why you offered that. As I said, I wasn't offering a general solution, I was showing a neat math trick.

But since you brought it up I did some more testing. The routine I just offered accepts the name of a variable on its commandline, not a string, so it accepts special characters in the variables. For example:

Set "String=&|<>"
Call :Strlen String

The result is 4.

From the CONSOLE commandline they need to be escaped, even in quotes:

strlen ^|^&^<^>
4
strlen "^|^&^<^>"
6

In the above usage the quotes are counted in the string length.

But those problems are a fault of the calling script, not of the string length routine. A simple modification of the input routine allows special characters from the console:

:: BEGIN SCRIPT :::::::::::::::::::::::::::::::::::::::::::::
@Echo OFF
SetLocal ENABLEEXTENSIONS

For /F "delims=" %%a in ('Echo.%*') Do Set "String=%%a"


Call :Strlen String
Set /A "StrLen=:StrLen"
Echo.Your string contains %StrLen% characters

Goto :EOF

:strlen
Set "%0="
For /F "tokens=1 delims=:" %%n in (
'(Echo.%~1^&Set %~1^&echo.^)^|FindStr /o "^"'
) Do Set /a %0=%0*-2+%%n
Goto :EOF
:: END SCRIPT :::::::::::::::::::::::::::::::::::::::::::::::


Frank

Message has been deleted

Frank P. Westlake

unread,
Nov 11, 2009, 8:31:42 PM11/11/09
to
"carlos" news:42619d02-8a16-49c9...@v25g2000yqk.googlegroups.com...

> code.bat hello
>
> the output is:
>
> Your string contains 31 characters.

Yes, I was aware of that when I posted the first message but then I was only showing the math trick. After spending time searching back through Usenet and testing various strings I simply forgot about that problem.

The problem is that it uses SET to send the string to FINDSTR, and it will also send other variables which begin with the same string. This is probably what you meant by "prefix". I didn't understand that at first.

I had another version which used ECHO|ECHO instead of ECHO|SET|ECHO and the same or a very similar math series, but I deleted it. Now I'll have to try to resurrect it.

Frank

01MDM

unread,
Nov 12, 2009, 8:10:24 AM11/12/09
to
With temporary file example:

@echo off
setlocal

set "string=Some String Here"
0>nul 1>temp.file set /p="%string%"

for %%i in (temp.file) do set len=%%~zi
del temp.file
echo %len%

Message has been deleted

I'm_HERE

unread,
Nov 12, 2009, 8:54:24 AM11/12/09
to

------------------8<--------------------

@echo off
:::::::::::::::::::::::::::::::::::::::::::::::
: Walid (I'm_HERE)
:::::::::::::::::::::::::::::::::::::::::::::::
echo\Input=Inputbox("String:","Input")>..vbs
echo\wsh.echo "set length="^&Len(Input)>>..vbs
cscript/nologo ..vbs>..bat
for %%. in (call del) do %%. ..bat ..vbs
echo\%length%
pause

------------------8<--------------------

Frank P. Westlake

unread,
Nov 12, 2009, 8:46:12 AM11/12/09
to
None of these have neat math tricks.

Frank

Tom Lavedas

unread,
Nov 12, 2009, 11:12:00 AM11/12/09
to
On Nov 11, 5:11 pm, "Frank P. Westlake" <frank.westl...@yahoo.com>
wrote:
> "Tom Lavedas"news:a97717b3-7a39-44d4...@15g2000yqy.googlegroups.com...
>
> See:http://groups.google.com/group/alt.msdos.batch.nt/browse_frm/thread/f...

>
> I'm not sure why you offered that. As I said, I wasn't offering a general solution, I was showing a neat math trick.

Sorry, your subject line said something about an "Interesting String
length Routine" and I jumped to the conclusion that that's what it was
all about. Silly me ;-)
_____________________
Tom Lavedas

PS. The 'math trick' still goes over my head - even after reading it
several times. I guess I'm dense.

Frank P. Westlake

unread,
Nov 12, 2009, 2:42:49 PM11/12/09
to
"Tom Lavedas" news:4afef9a4-9fe3-47c9...@37g2000yqm.googlegroups.com...

> ... your subject line said something about an "Interesting String
> length Routine" ...

Sorry, it was just a quick attempt to make it relevant. If I wrote "Neat Math Trick" it would have seemed off topic.

> The 'math trick' still goes over my head - even after reading it
> several times.

The word "trick" was applied only after this already became a mess. The interesting thing about the math is how the character offset of each of the three lines work together into a series equation, with the initial offset initializing the variable to zero. I had to take seven consecutive quarters of math during a three year period for a two year degree so maybe I saw more in it then there is.

By te way, in your string length routine is the search string necessary? I've been searching only for the beginning of the line (FindStr "^") and haven't seen any problems.

Frank

Dr J R Stockton

unread,
Nov 12, 2009, 9:12:36 AM11/12/09
to
In alt.msdos.batch.nt message <hdf5qk$rdu$1...@news.albasani.net>, Wed, 11
Nov 2009 12:05:47, Frank P. Westlake <frank.w...@yahoo.com> posted:

>Here is an interesting string length routine. The interesting thing about it is what values are manipulated to determine the length. This
>version doesn't handle special characters in the string so this isn't a good general solution, I just thought it was interesting how the math
>worked out.

If you were to set the composing right margin to the conventional 72
characters, your articles would be easier to read.

--
(c) John Stockton, nr London UK. replyYYWW merlyn demon co uk Turnpike 6.05.
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.

Tom Lavedas

unread,
Nov 12, 2009, 5:32:02 PM11/12/09
to
On Nov 12, 2:42 pm, "Frank P. Westlake" <frank.westl...@yahoo.com>
wrote:
> "Tom Lavedas"news:4afef9a4-9fe3-47c9...@37g2000yqm.googlegroups.com...
>
>
> By the way, in your string length routine is the search string necessary? I've been searching only for the beginning of the line (FindStr "^") and haven't seen any problems.
>
> Frank

No, I realized after I posted that it could be done with any match at
all, like this one ...

@echo off
setlocal
for /f "delims=:" %%a in (
'^(echo."%~1"^& echo.^)^|findstr /O .'
) do set /a Length=%%a-5
echo % for example %Length=%Length%
_____________________
Tom Lavedas

Frank P. Westlake

unread,
Nov 12, 2009, 5:29:31 PM11/12/09
to
"Dr J R Stockton" news:GnVx9fFUfB$KF...@invalid.uk.co.demon.merlyn.invalid...

> If you were to set the composing right margin to the conventional 72
> characters, your articles would be easier to read.

I'm sorry doctor but I cannot find the setting. It's Windows Mail. I've searched the menues, the user and machine registry, and the Internet. The only margin setting I've found is for UUencode, which I haven't been using. I've been using quoted-printable and that is required to wrap at 76 or less.

....'....1....'....2....'....3....'....4....'....5....'....6....'....7..
If you are referring to the text word wrap, not encoding word wrap, then
there is not a right margin. Intenet mail allows your text reader to
wrap paragraphs as you please. The only way I can see to make lines of
72 or fewer characters it to place a text ruler across the line and hit
ENTER at a suitable space, as I did with this paragraph.

If you are referring to the right column of the scripts I include with many of my messages then I normally do format them for 67 columns or less. It is sometimes a great job to do so but scripts are sensitive to white space, especially line endings.

If you have more guidance please offer it. I'm not certain I understand what you have tried to convey.

Frank

Frank P. Westlake

unread,
Nov 12, 2009, 5:47:40 PM11/12/09
to
"Tom Lavedas" news:35e1cc34-494e-49fe...@c3g2000yqd.googlegroups.com...

> for /f "delims=:" %%a in (
> '^(echo."%~1"^& echo.^)^|findstr /O .') do set /a Length=%%a-5

Very compact. You can trim it even one more character: left parenthises don't need to be escaped, only right parentheses.

Frank

Dr J R Stockton

unread,
Nov 13, 2009, 5:49:13 PM11/13/09
to
In alt.msdos.batch.nt message <hdi2lp$cuu$1...@news.albasani.net>, Thu, 12
Nov 2009 14:29:31, Frank P. Westlake <frank.w...@yahoo.com> posted:

>"Dr J R Stockton" news:GnVx9fFUfB$KF...@invalid.uk.co.demon.merlyn.invalid...
>
>> If you were to set the composing right margin to the conventional 72
>> characters, your articles would be easier to read.
>
>I'm sorry doctor but I cannot find the setting. It's Windows Mail. I've searched the menues, the user and machine registry, and the Internet.
>The only margin setting I've found is for UUencode, which I haven't been using. I've been using quoted-printable and that is required to wrap
>at 76 or less.

You are transmitting lines with up to 144 characters - clearly you do
have a margin set to 144. The human eye, however, is optimised for 45
to 72 characters per line; and Usenet convention is 72 for ordinary
text.

Nearly half of each of your text lines falls outside the window width
which suffices here for the text lines of the vast majority of other
users.

I have no idea about using Windows Mail, other than negative bargepole;
I use Turnpike, which is standards-compliant by default.

But every application, other than the most utterly primitive, intended
for text composition should have an easily-settable right margin.

If you compose in a proportionally-spaced font, the margin may be in
units of length; but Usenet is designed to be a fixed-pitch medium.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

Frank P. Westlake

unread,
Nov 13, 2009, 7:34:43 PM11/13/09
to
"Dr J R Stockton" news:2va9PEbpJe$KF...@invalid.uk.co.demon.merlyn.invalid...

> You are transmitting lines with up to 144 characters - clearly
> you do have a margin set to 144.

I might have found it. If so, it was set to 76, not 144. If my right column appears correct then others can use this if necessary:

HKEY_CURRENT_USER\Software\Microsoft\Windows Mail\News
VALUE: Message Plain Character Line Wrap

Frank

Frank P. Westlake

unread,
Nov 14, 2009, 5:23:41 PM11/14/09
to
"Frank P. Westlake" news:hdktvd$m37$1...@news.albasani.net...

> I might have found it.

> HKEY_CURRENT_USER\Software\Microsoft\Windows Mail\News


> VALUE: Message Plain Character Line Wrap

That didn't do it alone but one of these two helped:

"Message Plain Format MIME"=dword:00000000
"Message Plain Encoding Format"=dword:00000004


Frank


Dr J R Stockton

unread,
Nov 15, 2009, 12:31:16 PM11/15/09
to
In alt.msdos.batch.nt message <hdktvd$m37$1...@news.albasani.net>, Fri, 13
Nov 2009 16:34:43, Frank P. Westlake <frank.w...@yahoo.com> posted:

It didn't. And, while it may be recorded in the Registry, it ought to
be somewhere in the ordinary menus or other controls of the application
- perhaps marked on a Ruler.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

Message has been deleted

mokomoji

unread,
Nov 19, 2009, 5:44:40 PM11/19/09
to
@echo off
set bbb="....'....1....'....2....'....3....&'....4....'....5....'....
6....'....7.."
set bbb=%bbb:&=1%
:loof
call :addon %bbb%
if "%bbb%" equ "" echo %add% & goto :end
goto :loof
goto :end
:addon
set bbb=%~1
set bbb=%bbb:~1%
set /a add=%add%+1
goto :eof
:end
pause


'')a

u'r source very difficult

(''a

mokomoji

unread,
Nov 19, 2009, 5:55:03 PM11/19/09
to
setlocal type..~(__~)

@echo off
setlocal EnableDelayedExpansion
set bbb="....'....1....'....2....'..&..3....'....4....'....5....'....


6....'....7.."
set bbb=%bbb:&=1%

set bbb=%bbb:~1,-1%
:loof
set bbb=%bbb:~1%
set /a add=!add!+1
if "!bbb!" equ "" echo !add! & goto :end
goto :loof
:end
pause

0 new messages