How to modify the elements of an array?

We can add a new element or modify the value of an existing element by using its index. The output of the below program is ‘last element is 12’

@echo off 
set a[0]=5 
set a[1]=7   
set a[2]=9 
Rem Adding a new element at the array end 
Set a[3]=12 
echo The last element is %a[3]% 

Now let us look at modifying the existing value. In the below snippet, we assign a[2] as 14 and the third element of the array is now 14

Set a[2]=14

echo The new value of the third element of the array is %a[2]% 

The output of the above snippet results in the below behavior

The new value of the third element of the array is 14 

Fig 1.3 Modify Elements in an Array

Batch Script – Arrays

An array is a collection of elements of the same data type. The arrays are not explicitly defined as Batch Script types but can be used. The following items need to be noted when the same members are used in Batch Script. 

  • Each aspect of the same members needs to be defined by a set order.
  • A ‘for’ loop will be required to double the program values.

Similar Reads

How to create an array?

We can create an array using the command ‘set’. For example, set a[0] = 1 means we created an array whose first element of index 0 has a value of 1. Also, another way to define an array is by creating a list and then looping through it. Let us look at the below example....

How to Access an Array and its elements?

The easiest way to access an array is by using its Index. In the above example, the first element can be accessed as a[0], second with a[1], and so on. The index starts from 0 and ends with length(array)-1. Let’s look at the below example, where we have 2 elements in the array and we are accessing the values by their index....

How to modify the elements of an array?

We can add a new element or modify the value of an existing element by using its index. The output of the below program is ‘last element is 12’...

Iterating over an Array

Iterating over an array is achieved using for loop. Using for loop, we can access elements one by one. Let us look at the example below. /l is used here, as we move through the specified range and iterate through the Array....

Length of an Array

There is no predefined function to find the length of the array. We can use for loop and Iterate through the Array....

Creating Structures inside Arrays

We can also create structures inside an array. In the below example, Each of the variables defined using the default command (set) has 2 values ​​associated with each of the identical members. The variable i is set to 0 so that we can move through the structure and the loop will run 3 times. We constantly check the status of whether i value is equal to len value and if not, we enter with code. We can access each component of the structure using obj [%i%] notation....

Test Existence of an element

We use the if defined functionality to check if an element is in the array or not. %1 is the first argument from the invoking command line. %~1 means the quotes surrounding the arguments are removed....

Contact Us