Can a variable have multiple values?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alleypuppy
Posts: 82
Joined: 24 Apr 2011 19:20

Can a variable have multiple values?

#1 Post by alleypuppy » 28 May 2011 11:55

Hey guys,

I need to know if a variable can have multiple values rather than just one. I'm trying to make a file that calls for a string to be "start %variable%" and the variable needs to equal more than one value.

Forgive me if this is a simple question. I'm a total noob with Batch Files.

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Can a variable have multiple values?

#2 Post by nitt » 28 May 2011 12:11

alleypuppy wrote:Hey guys,

I need to know if a variable can have multiple values rather than just one. I'm trying to make a file that calls for a string to be "start %variable%" and the variable needs to equal more than one value.

Forgive me if this is a simple question. I'm a total noob with Batch Files.


I think you're looking for arrays. No, I don't think Batch has normal arrays. But you can make array-like variables like this:

Code: Select all

@echo off
set arry[0]=Hello
set arry[1]=,
set arry[2]=World
set arry[3]=!
echo %arry[0]%%arry[1]%%arry[2]%%arry[3]%
pause


But no, you can't like call on a variable and have 2 output, unless you want them side-by-side like "set variable=1 3".

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Can a variable have multiple values?

#3 Post by Ed Dyreen » 28 May 2011 15:00

I don't wan't to force anyone using macros, but really they are so easy to use !

push %~b onto %~a.array:
set "%%~a.Array=%%~b¦^!%%~a.Array^!" %\n2%

pop array
set "%%~a.Array=^!%%~a.Array:*¦=^!" %\n2%

see array
set /p "?=^!%%~a.Array^!" ^<nul %\n2%

You can also get elements, but that is a little more work. And I won't post if people are going to be negative about my comments again.

alleypuppy
Posts: 82
Joined: 24 Apr 2011 19:20

Re: Can a variable have multiple values?

#4 Post by alleypuppy » 28 May 2011 16:35

nitt wrote:
alleypuppy wrote:Hey guys,

I need to know if a variable can have multiple values rather than just one. I'm trying to make a file that calls for a string to be "start %variable%" and the variable needs to equal more than one value.

Forgive me if this is a simple question. I'm a total noob with Batch Files.


I think you're looking for arrays. No, I don't think Batch has normal arrays. But you can make array-like variables like this:

Code: Select all

@echo off
set arry[0]=Hello
set arry[1]=,
set arry[2]=World
set arry[3]=!
echo %arry[0]%%arry[1]%%arry[2]%%arry[3]%
pause


But no, you can't like call on a variable and have 2 output, unless you want them side-by-side like "set variable=1 3".

Hmmm... Is it possible to have multiple variables written in the command but the command will execute if only one is typed? For instance:

Code: Select all

set var1=iexplore
set var2=notepad
set var3=chrome

set /p command=^>
if /I "%command%"=="start %var1%, %var2%, %var3%" start %var1%%var2%%var3%

I know that this way doesn't work, but is there a way to make it work like this?

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Can a variable have multiple values?

#5 Post by nitt » 28 May 2011 16:48

Ed Dyreen wrote:I don't wan't to force anyone using macros, but really they are so easy to use !

push %~b onto %~a.array:
set "%%~a.Array=%%~b¦^!%%~a.Array^!" %\n2%

pop array
set "%%~a.Array=^!%%~a.Array:*¦=^!" %\n2%

see array
set /p "?=^!%%~a.Array^!" ^<nul %\n2%

You can also get elements, but that is a little more work. And I won't post if people are going to be negative about my comments again.


...Lol I really don't understand that... I don't really try to learn all the BrainF--- (that's a programming language) type stuff. Batch isn't that important to me.

alleypuppy wrote:
nitt wrote:
alleypuppy wrote:Hey guys,

I need to know if a variable can have multiple values rather than just one. I'm trying to make a file that calls for a string to be "start %variable%" and the variable needs to equal more than one value.

Forgive me if this is a simple question. I'm a total noob with Batch Files.


I think you're looking for arrays. No, I don't think Batch has normal arrays. But you can make array-like variables like this:

Code: Select all

@echo off
set arry[0]=Hello
set arry[1]=,
set arry[2]=World
set arry[3]=!
echo %arry[0]%%arry[1]%%arry[2]%%arry[3]%
pause


But no, you can't like call on a variable and have 2 output, unless you want them side-by-side like "set variable=1 3".

Hmmm... Is it possible to have multiple variables written in the command but the command will execute if only one is typed? For instance:

Code: Select all

set var1=iexplore
set var2=notepad
set var3=chrome

set /p command=^>
if /I "%command%"=="start %var1%, %var2%, %var3%" start %var1%%var2%%var3%

I know that this way doesn't work, but is there a way to make it work like this?


So you want to check if only one variable is typed, then only do the command if one variable is typed?

Just check like if "%var2%" and "%var3%" are NEQ to "". If true, then skip the execute.

Like

Code: Select all

blahblah

if "%var2%" NEQ "" goto eof
if "%var3%" NEQ "" goto eof

blahblah

:eof


That will make it so if there is anything in the var3 and var2 variable then it will just end the code.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Can a variable have multiple values?

#6 Post by orange_batch » 28 May 2011 17:21

Ed Dyreen wrote:I don't wan't to force anyone using macros, but really they are so easy to use !

push %~b onto %~a.array:
set "%%~a.Array=%%~b¦^!%%~a.Array^!" %\n2%

pop array
set "%%~a.Array=^!%%~a.Array:*¦=^!" %\n2%

see array
set /p "?=^!%%~a.Array^!" ^<nul %\n2%

You can also get elements, but that is a little more work. And I won't post if people are going to be negative about my comments again.


Who's being negative? :<

Though, easy? I still haven't a clue about using this macro business! lol, that syntax looks nutty even for DOS. I should probably study dbenham's thread.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Can a variable have multiple values?

#7 Post by Ed Dyreen » 28 May 2011 18:06

@orange_batch
Though, easy? I still haven't a clue about using this macro business! lol, that syntax looks nutty even for DOS. I should probably study dbenham's thread.


Who's being negative? :<

irrelevant.

[SOLVED] SET /a -- Random Number?
http://www.dostips.com/forum/viewtopic.php?f=3&t=1817

Batch "macros" with arguments
http://www.dostips.com/forum/viewtopic.php?f=3&t=1827

I am currently writing a set of macros, studying jeb's &Benhams posts.
you can do things like:
call :MacroDef.CMD rem only once !

%@forA% ( '1¦1000¦return' )%@Random%

It then gets you the result, faster than any:
call :Random 1 1000 return
Simply because there is no call at all ! a macro is like a function variable expanding in memory. Complex is not always better but in this case well :P it really is !

There is a problem however, the number of characters available for variables is limited !
but i found a way around the problem and I am getting of topic seriously :
You simply call a funtion from within the macro with arguments on what do next.
Then this function calls the next macro etc..

Just follow the links above ↑↑↑
Last edited by Ed Dyreen on 28 May 2011 19:00, edited 7 times in total.

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Can a variable have multiple values?

#8 Post by nitt » 28 May 2011 18:10

orange_batch wrote:
Ed Dyreen wrote:I don't wan't to force anyone using macros, but really they are so easy to use !

push %~b onto %~a.array:
set "%%~a.Array=%%~b¦^!%%~a.Array^!" %\n2%

pop array
set "%%~a.Array=^!%%~a.Array:*¦=^!" %\n2%

see array
set /p "?=^!%%~a.Array^!" ^<nul %\n2%

You can also get elements, but that is a little more work. And I won't post if people are going to be negative about my comments again.


Who's being negative? :<

Though, easy? I still haven't a clue about using this macro business! lol, that syntax looks nutty even for DOS. I should probably study dbenham's thread.


Ya. I just don't bother with any of the crazed syntax. There's really not a big point in it.

You're not alone. I actually don't suggest learning that syntax. Since Batch is really only just for fun, and for understanding computers better, you really shouldn't spend too much time learning it all. It's just not worth it.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Can a variable have multiple values?

#9 Post by orange_batch » 28 May 2011 18:50

lol nitt, I understand the syntax very well, it's the logic of his particular method that I'd have to study to follow. But I disagree, batch has it's drawbacks but it's deceptively powerful. It runs well enough in the right hands. In any case, Command Prompt's purpose is to be a simple interface.

It depends how one looks at it. To me, Command Prompt uses very little memory and if it produces the intended result, I see no need to use a different language.

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Can a variable have multiple values?

#10 Post by nitt » 28 May 2011 18:57

orange_batch wrote:lol nitt, I understand the syntax very well, it's the logic of his particular method that I'd have to study to follow. But I disagree, batch has it's drawbacks but it's deceptively powerful. It runs well enough in the right hands. In any case, Command Prompt's purpose is to be a simple interface.

It depends how one looks at it. To me, Command Prompt uses very little memory and if it produces the intended result, I see no need to use a different language.


...Are you seriously saying that you know so much Batch that no other language is needed?

I don't really think you get how Batch and every other language works. Batch is designed for only specific purposes. It's not even considered a programming language, because it's not really interpreted by the computer like others languages are.

I understand this, and I'm not even an application programmer. I'm a web programmer. I just happen to know a few application programming languages.

Batch is rarely used professionally. Sometimes it is to execute Java applications, or help execute other applications. But that's about it.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Can a variable have multiple values?

#11 Post by orange_batch » 28 May 2011 19:36

nitt wrote:...Are you seriously saying that you know so much Batch that no other language is needed?

No, I didn't say that.

nitt wrote:I don't really think you get how Batch and every other language works. Batch is designed for only specific purposes. It's not even considered a programming language, because it's not really interpreted by the computer like others languages are.

Yes, that's right. Batch is a way of automating tasks, and it can work with file systems, text processing (to a limit) and more, I'm sure I don't need to explain that. But I would turn the question, I've developed strong applications in batch as others here have.

nitt wrote:I understand this, and I'm not even an application programmer. I'm a web programmer. I just happen to know a few application programming languages.

Batch is rarely used professionally. Sometimes it is to execute Java applications, or help execute other applications. But that's about it.

Microsoft uses it in deployment of Windows Service Packs among other things. :roll:

Ed Dyreen wrote:%@forA% ( '1¦1000¦return' )%@Random%

It then gets you the result, faster than any:
call :Random 1 1000 return
Simply because there is no call at all ! a macro is like a function variable expanding in memory. Complex is not always better but in this case well :P it really is !

There is a problem however, the number of characters available for variables is limited !
but i found a way around the problem and I am getting of topic seriously :
You simply call a funtion from within the macro with arguments on what do next.
Then this function calls the next macro etc..

Cool, I get it. Like you said though, it's mostly only useful for huge scripts over 100 KB+ right?

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Can a variable have multiple values?

#12 Post by Ed Dyreen » 29 May 2011 01:07

Cool, I get it. Like you said though, it's mostly only useful for huge scripts over 100 KB+ right?

yes, the idea with larger scripts is usually abstraction, so the the number of errors one makes due to repetition reduces. This is accomplished with functions.
Macro's are LIKE functions that is, it's quite easy to make a variable into a function which then becomes a macro.
The clocked results are impressive compared to functions.
But the macro definition has to end when the macro is full or you'll get rubbish when you try to use it.

I brought up the idea once &benham took it to quite a level, I don't fully understand, but we use quite a differend technique,
benham defines the macro's in disabledelayedexpansion &uses a derefferencer,
I use enabledelayedexpansion because I'm afraid not to be able to assign &get variables within one for loop.
But that is just because I don't fully understand it yet.. :(

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Can a variable have multiple values?

#13 Post by dbenham » 29 May 2011 15:22

orange_batch wrote:Cool, I get it. Like you said though, it's mostly only useful for huge scripts over 100 KB+ right?
Not at all!. It can be extremely useful even with small scripts, although the performance benefit is certainly magnified within large scripts. Look at the timings I posted in the batch thread for a small script - a factor of 3 to 4 is nothing to sneeze at, especially if you are making calls in a loop. And the difference I experienced when running on a slow shared network drive is jaw dropping.

Besides speed, another major benefit is the ability to use macros without including the code in each script. Simply define the macros before issuing a SETLOCAL and the macro definitions become resident in the DOS environment. One simple strategy is to have the macro library script define a macroLibIndicator variable. Then at the top of any batch that needs a macro use

Code: Select all

 if not defined macroLibIndicator call macroLib.bat
Another strategy is to have the library loaded automatically via (? what replaced AUTOEXEC.BAT in Windows?).

I'm still discovering interesting uses for the batch macro with arguments concept.

Dave Benham

Post Reply