1

I've recently took up a project to learn scripting in batch on Windows.

I want to write a code that allows me to create an array that I can print, add to and remove from.

In my head I see it something like:

set List = [Bob, Adam, Steve] ::Creates an array
echo What is your name?
set /p name= 
list.add(List + name) ::Adds name to list
echo Hello List[3] ::Prints the 4th name in array
echo My name is List[0] ::Prints the 1st name in array

That is just a rough sketch, I know it will not work but am I on the right lines? If so what needs to be changing?

Squashman
  • 13,649
  • 5
  • 27
  • 36
JellyBaby
  • 59
  • 1
  • 8
  • What language is this? There are many batch file interpreters. – DYZ Nov 08 '18 at 17:40
  • That I wouldn't know - I just use ".bat" ending on a Notepad ++ application, if that's any help :) – JellyBaby Nov 08 '18 at 17:42
  • So, it is Windows? Why don't you say so? – DYZ Nov 08 '18 at 17:43
  • There we go - I've edited it - sorry, didn't know that there are different version of batch interpreters. – JellyBaby Nov 08 '18 at 17:45
  • 1
    It doesn't create an array, just a delimited string. To add to the string you'd usually use `Set "List=Bob,Adam,Steve"`, `Set /P "name=What is your name? "`, `Set "List=%List%,%name%"`. Batch files do not have array capacity, only 'pseudo arrays', to reference each name in the list, you'd essentially parse it and assign each substring to a different variable. – Compo Nov 08 '18 at 17:47
  • 1
    @DYZ - I would be very interested in you naming literally anything other than cmd. – SomethingDark Nov 08 '18 at 17:54
  • @SomethingDark How about bash? It was not even clear that the OP meant Windows. – DYZ Nov 08 '18 at 17:56
  • 1
    @DYZ - Bash is _NOT_ batch. Just because they are both scripting languages doesn't mean they can both be called "batch." The two languages have practically nothing in common. Plus, the [batch-file] tag is exclusively meant for Windows CMD, and `set /p` and `::` are both exclusive to CMD. – SomethingDark Nov 08 '18 at 17:57
  • @SomethingDark Even if I accept your definition, there was also command.com - yet another batch interpreter, different com cmd.exe. More on this [here](https://stackoverflow.com/questions/5079180/difference-between-batch-and-bash-files). – DYZ Nov 08 '18 at 18:03

2 Answers2

1

That isn't an array, that's a list (kind of; Batch knows only one type of variable: string). Here is the syntactical correct version of your pseudo-code:

set "List=Bob,Adam,Steve" 
set /p "name=What is your name? "   
set "list=%list%,%name%"
for /f "tokens=4 delims=," %%a in ("%list%") do echo Hello %%a
for /f "tokens=1 delims=," %%a in ("%list%") do echo My name is %%a

For your interest: there is a detailed article about Arrays, linked lists and other data structures in cmd.exe (batch) script

EDIT
for your "inside a for /l loop" question: for is a bit picky about parameters for the tokens part, but that can be solved by using call:

@echo off
set "List=Bob,Adam,Steve" 
set amount=3 
for /l %%i in (%amount%; -1; 1) do call :sub %%i
goto :eof

:sub
for /f "tokens=%1 delims=," %%a in ("%list%") do echo Hello %%a 
goto :eof
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • What is the difference between: `set "List=Bob, Adam, Steve"` and: `set list="Bob, Adam, Steve"` Does the speech marks really make a difference? – JellyBaby Nov 08 '18 at 18:37
  • 2
    yes, it does. With `set var="value"`, the value will contain the quotes. `set "var=value" does the same as `set var=value`, but protects you from accidental spaces at the end. Also it protects from poison characters: `set "var=alpha&beta" sets the value to `alpha&beta`, while `set var=alpha&beta` sets the value to `alpha` and tries to execute `beta` – Stephan Nov 08 '18 at 18:44
  • Just a quick question to see if it's possible, having your script in a for loop like so: `set "List=Bob,Adam,Steve" set amount=3 for /l %%i in (%amount%; -1; 1) do ( for /f "tokens=%%i delims=," %%a in ("%list%") do echo Hello %%a )` So the index of the list changes, so the name changed whilst it's going through the list? – JellyBaby Nov 08 '18 at 21:40
1

Here's an alternative example:

@Echo Off
SetLocal EnableDelayedExpansion
Set "#=0"
Set "List=Bob,Adam,Steve"
Set /P "name=What is your name? "
Set "List=%List%,%name%"
Set "List[!#!]=%List:,="&Set/A #+=1&Set "List[!#!]=%"
Rem Show all pseudo array items
Set List[
Rem Prints the 4th name in pseudo array
Echo My name is %List[4]%
Rem Prints the 1st name in pseudo array
Echo My name is %List[0]%
Pause
Compo
  • 36,585
  • 5
  • 27
  • 39
  • This one looks a tad more complicated than the previous answer. Plus - I don't really understand what `SetLocal EnableDelayedExpansion` is for, so far I know that using it won't allow me to print "!" when using `echo`. – JellyBaby Nov 08 '18 at 19:25