Adding array element in KSH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding array element in KSH
# 1  
Old 12-17-2008
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[0]} + times[1] + times[2]))

I would like an easier method of adding the elements...

Thanks!

Mike

Last edited by Shoeless_Mike; 12-17-2008 at 03:44 PM.. Reason: syntax error
# 2  
Old 12-17-2008
Bump - please help
# 3  
Old 12-17-2008
Code:
$ cat add_array.ksh

#!/bin/ksh

set -A times 1 2 3 4 5 6
TIMESCOUNT=${#times[*]}

print "There are $TIMESCOUNT times elements"
print "times array = ${times[@]}"

TIMESIDX=0
SUM=0
while (( TIMESIDX < TIMESCOUNT ))
do
   SUM=$(($SUM+${times[TIMESIDX]}))
   TIMESIDX=$(($TIMESIDX+1))
done

print "SUM=$SUM"

exit 0

$ ./add_array.ksh
There are 6 times elements
times array = 1 2 3 4 5 6
SUM=21

# 4  
Old 12-17-2008
Much obliged.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Adding to an array in an external file, and adding elements to it.

I have an array in an external file, "array.txt", which contains: char *testarray={"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};I want to be able to add an element to this array, and have that element display, whenever I call it, without having to recompile... (29 Replies)
Discussion started by: ignatius
29 Replies

2. Shell Programming and Scripting

Adding character and append last element of second column

Hi, could you help me in processing this file under bash? I need to add some text to the first line and then append the last element of the second columns. The input file is tab separated while the output should be space separated. input file is 1.00E-02 2.00E-02 4.465E+17 2.00E-02 3.00E-02... (4 Replies)
Discussion started by: f_o_555
4 Replies

3. Shell Programming and Scripting

Adding an element to a bash array with a variable

Hello, I have a simple task and I am having some trouble with the syntax. I have a variable with an assigned value, CMD_STRING='-L 22 -s 0 -r -O -A i -N 100 -n' I would like to add that variable to an array. As far as I have been able to look up, the syntax should be something like, ... (4 Replies)
Discussion started by: LMHmedchem
4 Replies

4. 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

5. 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

6. 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

7. Shell Programming and Scripting

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 then print all arrayA elements separated by comma. Appreciate your help. (9 Replies)
Discussion started by: ejianu
9 Replies

8. 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

9. 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

10. 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
Login or Register to Ask a Question