2

I Wanna to know what is the Arrays in Batch Scripting and how its Work? and Why we need using Arrays in Batch File?

i know we can use SET and FOR command to create an Arrays but not know why we must use it at all.

Thanks a lot!

@echo off
set len=3
set obj[0].Name=Joe
set obj[0].ID=1
set obj[1].Name=Mark
set obj[1].ID=2
set obj[2].Name=Mohan
set obj[2].ID=3
set i=0
:loop
if %i% equ %len% goto :eof
set cur.Name=
set cur.ID=
for /f "usebackq delims==. tokens=1-3" %%j in (`set obj[%i%]`) do (
set cur.%%k=%%l
)
echo Name=%cur.Name%
echo Value=%cur.ID%
set /a i=%i%+1
goto loop

Why we must use Such Code in Batch Script?

Mohammad Barghamadi
  • 107
  • 1
  • 1
  • 10
  • Who says you have to? Also, there really _aren't_ arrays in batch files (not as they exist in other languages); there's just a construct you can use to make it look like there is... all the above is doing is creating separate environment variables (e.g. `obj[0].Name`, and `obj[2].ID`) and using name-pasting techniques to make it look _a bit like accessing an array_. It presumably can be useful (though I've never needed it), but remember it's not full-blown arrays. – TripeHound Feb 13 '17 at 12:52
  • The _array data structure_ is a powerful tool that allows to solve a wide range of problems in a simpler way. There are tons of examples of this point, so I invite you to look for they, like [this introduction](https://www.teamten.com/lawrence/programming/intro/intro11.html) or the [Wikipedia article](https://en.wikipedia.org/wiki/Array_data_structure). Note that this description don't refers to any programming language, so the benefits of using arrays also apply to Batch files. – Aacini Feb 13 '17 at 13:35
  • @Aacini I just need your Great Info about Array, thank you – Mohammad Barghamadi Feb 13 '17 at 14:35

1 Answers1

6

You don't "must" use arrays, you "can" use the tools the language provides or allows you to use, but in this case, the tool doesn't even exist.

The syntax of batch files does not include arrays

BUT, we can simulate their exist (and binary trees, linked lists, defined types, ...). The batch parser only see variables, a variable name to allow the retrieval of a stored information, it is our naming of them and how we code the store/retrieve operations what creates our perception of an array.

Community
  • 1
  • 1
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • I invite you to read [my opinion](http://stackoverflow.com/questions/10544646/dir-output-into-bat-array/10569981#10569981) about this "answer" (that IMHO should be just a comment)... – Aacini Feb 13 '17 at 13:47
  • 2
    @Aacini, sorry, I can not agree with your point of view. A question as *"What is the Array In Batch Script and how its work?"* needs this kind of answer (of course, including the link to your answer). Just my opinion, but I think data types and data structures are the foundations of software development. Pedantic or not, *a developer* (and I will consider as a developer anyone asking and interested in data representation) *should not misunderstand or unknow how or where data is stored*. – MC ND Feb 13 '17 at 15:32