ksh insert element in array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh insert element in array
# 1  
Old 04-23-2012
ksh insert element in array

Hi all,

I need help with the following scenario in ksh.

If the number of elements contained by arrayA is 11 I need to insert a zero as the element arrayA[5] then print all arrayA elements separated by comma.

Appreciate your help.
# 2  
Old 04-23-2012
Which ksh implementation and what did you try so far?
# 3  
Old 04-23-2012
Version M-11/16/88i

Reading my post above I think I wasn't very specific. I do not want the 6th element to be replaced with zero. What I need is to insert a zero as the 6th element and shift everything that follows to the right.

What I tried was to split it into two arrays, but I have problems when I assign array values.
# 4  
Old 04-23-2012
Can you show the code you were trying please? Use [code] and [/code] tags, when doing so, thanks.
# 5  
Old 04-23-2012
I believe that with ksh88 you'll need to loop and set/adjust the elements manually. As zaxxon said: post your code and we will try to suggest a fix.
# 6  
Old 04-23-2012
You may have to count your way along the array with two pointers. A source address and a target address for a replacement array. The source address gets incremented each time through a loop to copy an element from one to the other, but within the loop there would be an extra increment to the target address and an insert of the zero in the position you want. Something like this may assist:-

Code:
#!/bin/ksh
set -a source_array
set -a target_array

for i in 1 2 3 4 5 6 7 8 9 10 11 12
do                                  # Just loading something into the source.
   source_array[$i]="a${i}a${i}"    # You should already have your data loaded.
done

src_addr=1 ; trg_addr=1

while [ $src_addr -le ${#source_array[*]} ]
do
   target_array[$trg_addr]="${source_array[$src_addr]}"
   echo "Setting target array element $trg_addr from source array element $src_addr value ${source_array[$src_addr]}"
   ((src_addr=$src_addr+1))
   ((trg_addr=$trg_addr+1))

   if [ $trg_addr = 6 ]              # For the required insert
   then
      target_array[$trg_addr]=0    # Set value
      echo "Setting target array element $trg_addr as zero."
      ((trg_addr=$trg_addr+1))   # ... and move pointer along one
   fi
done


I hope that this helps. It may well not be the most efficient way, but it depends how big an array you have and how many times you will run this.




Robin
Liverpool/Blackburn
UK
# 7  
Old 04-23-2012
My thought was to shift the elements of the array over by one until we hit the one that needs to be zero. I assumed the sixth element based on the index as arrays are zero-based. You may have to adjust accordingly:
Code:
$ cat x
#!/bin/ksh

## Create the starting array with 11 elements.  Arrays are
## zero-based, so they would be elements 0-10.
set -A arrayA 1 2 3 4 5 6 7 8 9 10 11

## arrayA_pointer starts out equal to the number of elements in the array
## (11), thus points 1 past the end (arrayA[11] does not exist yet).
integer arrayA_pointer=$((${#arrayA[@]}))

integer ctr=0
typeset -ir INSERT_POINT=6

## Show the starting array.
print "Before: [${arrayA[@]}]"

## Loop backwards through the elements, shifting them to the value
## of the one before it, until we come to the element that needs
## to be set to 0.
while (( $arrayA_pointer > $INSERT_POINT )); do
  arrayA[${arrayA_pointer}]=${arrayA[${arrayA_pointer}-1]}
  (( arrayA_pointer=arrayA_pointer-1 ))
done

## We hit the element that needs to be set to 0.
arrayA[$arrayA_pointer]=0

## Loop through the processed array, printing out each element.
printf "After: "
while (( ${ctr} < ${#arrayA[@]} )); do
  if (( ${ctr} + 1  == ${#arrayA[@]} )); then
    printf "%s\n" ${arrayA[$ctr]}
  else
    printf "%s, " ${arrayA[$ctr]}
  fi
  (( ctr=ctr+1 ))
done

exit 0
$ x
Before: [1 2 3 4 5 6 7 8 9 10 11]
After: 1, 2, 3, 4, 5, 6, 0, 7, 8, 9, 10, 11
$


Last edited by gary_w; 04-23-2012 at 02:38 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to insert a CSV within xml element tag using Python?

Hi Team, I have a CSV file which I have to read through and needs to insert the content within an XML file using Python ONLY ( as most of the code base we have in python only). I managed to find the first part, missing how to insert to XML under "specific" tags. cat input.csv... (0 Replies)
Discussion started by: panyam
0 Replies

2. UNIX for Advanced & Expert Users

Array Element

This question is for someone that's more familiar with Array Element. I need to know if the maximum array element that can be assigned is 1024 and if its so, Is there a workaround solution when the counter exceeded 1024? param_array="$param_nam" counter=$counter+1 #to avoid space... (3 Replies)
Discussion started by: cumeh1624
3 Replies

3. Shell Programming and Scripting

File names as array element in ksh

Hi, I have an ksh array(ARR). the elements to the array are file names. i need to go to each file in the array and manipulate the records. for name in ${files}; do ---this loop is for all the file names in the array for i in $(wc -l < $name); do --this loop is for all the records in... (20 Replies)
Discussion started by: usrrenny
20 Replies

4. Shell Programming and Scripting

How to insert an array element within regex?

Hello to all, I'm trying to separate the string "str" using a regex within match function. The substrings that I want to separate, begin with 22, 23, 24 or 25 and followed by 12 or 14 characters. And I want to replace 22 with MJS, 23 with UYT, 24 with WER and 25 with PIL. For this string... (4 Replies)
Discussion started by: Ophiuchus
4 Replies

5. Shell Programming and Scripting

Not able to call an element from an array in ksh

Hi, I have: # Initialize variables #!/usr/bin/ksh FILENM=$1 INDEX=0 # read filename echo "You are working with the Config file: $FILENM" while read line do echo $line data=$line ((INDEX=INDEX+1)) done <"$FILENM" (3 Replies)
Discussion started by: Marc G
3 Replies

6. Emergency UNIX and Linux Support

Assigning zero to element of ksh array.

set -A matched #find referenced files. for i in ${file_names_html} do counter_j=0 for j in ${file_names_minus_index} do match=`cat $i | grep... (1 Reply)
Discussion started by: robin_simple
1 Replies

7. Shell Programming and Scripting

Problem to initialize ksh array when first element includes hyphen

Hi I'm trying to create an array with variable including hyphen but ksh refuses the first element set -A allArgs set +A allArgs ${allArgs} -all set +A allArgs ${allArgs} -date set +A allArgs ${allArgs} test ./test.ksh: -all: bad option(s) It happens only when first element is like... (4 Replies)
Discussion started by: gdan2000
4 Replies

8. Shell Programming and Scripting

remove an element from array

I need to remove an element from the below array variable TABLENAME. #!/bin/ksh set -A TABLENAME "mirf roxar keke mirs" echo "the array is ${TABLENAME}" If i need to remove say keke and have the final TABLENAME as below, how this could be achieved. Pls throw some light. echo "Modified... (3 Replies)
Discussion started by: michaelrozar17
3 Replies

9. Shell Programming and Scripting

Shift array element

I want to delete and 0th element of array in shell scrpit and also shift all others to one level up. (2 Replies)
Discussion started by: darshakraut
2 Replies

10. Shell Programming and Scripting

Adding array element in KSH

All, I would like to add the first 10 elements of an array. Here is how I am doing it now (only included first few add ops): #!/usr/bin/ksh ###Grab the array values out of a file### TOTAL=`awk '/time/' /tmp/file.out | awk '{print $4}'` set -A times $TOTAL SUM=$((${times} + times... (3 Replies)
Discussion started by: Shoeless_Mike
3 Replies
Login or Register to Ask a Question