I've been playing around with a kind of variable (which I have no idea what the name is).
I'm going to use the time system variable as an example.
%time:~0,5%
That shows the first five digits of the variable, but why does 0 represent the first character in the variable? What the heck? (mindblown) Shouldn't it be 1, not 0?
And what does the second digit, the 5 represent? Does it indicate the position of some letter or number in a variable, or is it just the number of characters to display?
I've really never thought about this, but now its just bugging me...
And what does JUST a single digit do? %time:~5%, whats the purpose of that?
Thanks, bros.
._James_.
Help w/ understanding %var:~x,y%
Moderator: DosItHelp
Re: Help w/ understanding %var:~x,y%
Code: Select all
@echo off
echo %time%
echo %time:~0,5% - Skip zero from the start of the time environment variable and take the next 5 characters.
echo %time:~5% - Skip 5 characters from the start and return the rest.
echo %time:~-5% - Return 5 characters from the end of the variable.
echo %time:~2,-5% - skip 2 characters from the start and also 5 characters from the end. It returns a bit from the middle.
pause
-
- Posts: 2
- Joined: 31 Dec 2012 00:14
Re: Help w/ understanding %var:~x,y%
Ohhhh. So the first number is how many digits to SKIP? huh.
Re: Help w/ understanding %var:~x,y%
That is definitely a good way to think about it. But it is language specific.
Another way to think about it is convention: Pretty much all languages have the concept of substring operations and array referencing. There are generally two conventions:
0 based indexing - The first character in the string (or element of an array) is position 0
1 based indexing - The first character in the string (or element of an array) is position 1
A given language does not need to be consistent. Some languages may use 1 based indexing for string position and 0 based indexing for array indexing.
I can't remember which language (VB perhaps?), but I know there is at least one that allows you to choose whether you want to use 0 or 1 based indexing for arrays.
The important thing is you must learn what the convention is for your particular language, and work with it.
Dave Benham
Another way to think about it is convention: Pretty much all languages have the concept of substring operations and array referencing. There are generally two conventions:
0 based indexing - The first character in the string (or element of an array) is position 0
1 based indexing - The first character in the string (or element of an array) is position 1
A given language does not need to be consistent. Some languages may use 1 based indexing for string position and 0 based indexing for array indexing.
I can't remember which language (VB perhaps?), but I know there is at least one that allows you to choose whether you want to use 0 or 1 based indexing for arrays.
The important thing is you must learn what the convention is for your particular language, and work with it.
Dave Benham
-
- Posts: 20
- Joined: 21 Dec 2012 13:36
- Location: United States
Re: Help w/ understanding %var:~x,y%
DosTips has a very helpful page in reference to String Manipulation here: http://www.dostips.com/DtTipsStringManipulation.php
Re: Help w/ understanding %var:~x,y%
My Drill Sergeant told me several times doing push ups that zero was a number and to start over by counting from zero.
Our Print Software uses a Zero Offset but our data programming software does not. So as Dave has explained you have to remember which programming language or software in my case that you are using to remember how to reference your fixed data.
Our Print Software uses a Zero Offset but our data programming software does not. So as Dave has explained you have to remember which programming language or software in my case that you are using to remember how to reference your fixed data.