Untitled Document
Programs on Arrays
1. Program - To Declare and print the array content Click Here
In bash or Korn shell arrays are created automatically when we use the following syantax
Name[Index]=Value
  • Where Name could be any valid name for the array
  • Index is any number greater than or equal to zero
  • Value - could be any number or string you want to assing to the array index
  • To access array element put array in curly brackets { } and prefix it with $ symbol e.g ${array[index]}
Lets see the below program To declare an array and print the array content
       #!/bin/ksh 
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To Declare and print the array content
       
       arr[0]="Software Associate"
       arr[1]="Software Engineer"
       arr[2]="Sr. Software Engineer"
       arr[3]="Associate Consultant"
       arr[4]="Consultant" 
              
       echo ${arr[1]}  
       
2. Program - To Declare and Intialize the array . Click Here
In above example we have intialised each element separately , so rather than doing that lets declare and intilaise the array element in bracket separated by white space .
       #!/bin/ksh 
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To Declare and Intialize the array 
     
     set -A arr Mangesh Mohan Meena Mayura 
     echo  ${arr[@]} 
       
Lets Understand the code line by line
  • Where set -A Korn Shell Syntax for declaring an array
  • arr is the name of an array
  • after array name the 4 values are assigned in array
  • ${arr[@]}To Print array content where @ means print all the values of array
You can use * instead of @ in the array to print all the elements from an array
3. Program - To Check whether an array is blank or not Click Here
       #!/bin/ksh 
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To Check whether an array is blank or not
       
       set -A arr Mangesh Mohan Meena Mayura 
       echo  ${arr[@]}                     
        
       if [[ ${#arr[@]} -gt 0 ]]; then
          echo "Array has element"  
       else 
          echo "Array is empty"
       fi
             
       
  • In above example we have determined the length of the array using ${#arr[@]} which we have used in if statement
  • Gneral Syntax to determine the Length of Array will be like ${#arrayname[@]}
4. Program - To read a file and put all the file contents in the array Click Here
       #!/bin/ksh 
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # To read a file and put all the file contents in the array
       
          i=0          
         # while loop for reading file and storing data in array 
         while read line
         do
	         emp[$i]="$line"
	         i=`expr $i + 1`     
         done < emp123.csv
         
         # for loop for reading array
         
         for arrayval in "${emp[@]}"
         do
                  echo $arrayval
         done
      
5. Program - To add an element to an existing array Click Here
       #!/bin/ksh 
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To add an element to an existing array
       
          arr=("${arr[@]}" "Soham" "Meera")
          echo "${arr[@]}"                  
      
  • In above example we have used aleady existing array with name arr , so first we will print entire array content and at last we will add the new element in quotes , separated by whitespace
  • Next is store the values back to array by arr=("${arr[@]}" "Value1" "Value2")
6. Program - To delete an element from an array Click Here
       #!/bin/ksh 
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To delete an element from an array
       
       unset arr[4]
       echo ${arr[4]}
       
       echo ${arr[*]}          
      
      
  • To Delete an element from an array use unset command followed by array index e.g unset arrayname[index]
  • To delete an entire array just pass the array name to the unset command unset arr
7. Program - To read an element of an array by its Index Click Here
       #!/bin/ksh 
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To read an element of an array by its Index   
           
       arr=("Mangesh1" "Mangesh2" "Madhav" "Mithila" "Meera" "Maithili")
       echo ${arr[*]:2:3}          
      ---------------------------------------------------------------------------------------------------
      Output - Madhav Mithila Meera 
      
  • The above example prints the 3 element starting from position 2 of the array
  • The :2 indicates index position from where we have to start and :3 indicates how may array we want to extract from the given position ( in our case we will extract 3 elements)
8. Program - print length of an element from an array Click Here
       #!/bin/ksh 
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - Extarct the Characters from an array specified in index
       
       arr=("Mangesh1" "Mangesh2" "Madhav" "Mithila" "Meera" "Maithili")
       echo ${arr[4]:0:3}
     -------------------------------------------------------------------------------------    
      output - Meer
      
  • To Delete an element from an array use unset command followed by array index e.g unset arrayname[index]
  • To delete an entire array just pass the array name to the unset command unset arr
Untitled Document