arrays and two values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting arrays and two values
# 1  
Old 09-16-2011
arrays and two values

I have requirement where I need to capture the highest values of
items from a feed that runs for N hours.

For example lets assume my data looks like this

first feed
========
appples 10
oranges 20
pears 14

second feed
==========
apples 5
oranges 30
pears 1

Last feed
=======
apples 6
oranges 1
pears 4

At the end of my script this would be my output

apples 10
oranges 30
pears 14

I know I can write this all out to a file and than sort the file and look
for the highest values that way but I was wondering if this can be done
with an 2 dimensioal array if ksh supports it or with a more elegant
solution as writing all this data to a file may file up my file system.

my code would need to look something like this

Code:
 
while [ now -lt done_time]
do
   get_fruit | read data 
   eval $data
 
   keep the highest value for each fruit       
done
 
print out each fruit and the highest values

My solution does not need to be an array but I am really trying to
avoid writing data to files if possible until the very end...

Thanks for any suggestions
# 2  
Old 09-16-2011
ksh has better than 2d arrays, it has associative arrays. You can do ARR["apples"]=30

We detail on how this "fruit-daemon" provides its information to show you how to use that information, naturally.
# 3  
Old 09-16-2011
Thanks thats exactly what I was looking for. I will search how to set the values dynamucally
# 4  
Old 09-16-2011
Code:
$ nawk '{print $0 > $1".txt"}' feed*

The above command will create three .txt files ( apples.txt, orange.txt, pears.txt )

Code:
$ for i in *.txt; do sort -n -r -k2 $i | head -1; done
apples 10
oranges 30
pears 14

# 5  
Old 09-16-2011
Quote:
Originally Posted by BeefStu
Thanks thats exactly what I was looking for. I will search how to set the values dynamucally
How to set the values is easy. ARR[$VAR]=$VALUE

How you get the data depends entirely on what you're doing.
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 09-16-2011
It appears that assoc arrarys are not supported on AIX 6.1.. Thanks for
your help. It's a good feature
# 7  
Old 09-16-2011
For the fourth time, where to go from here depends on how your data's retrieved.

How's it retrieved?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare two arrays by values [BASH]

I have 2 arrays of values for example A1 ={10 15 3 21} A2 ={10 15 3 22} I need to check which one has greater values. However: A1 ={10 15 3 21} A2 ={10 15 3 21 3} - this one would be greater. A1 ={10 15 5 21} - this one greater A2 ={10 15 3 21} Basically, I want to compare patch... (6 Replies)
Discussion started by: Jutsimitsu
6 Replies

2. Shell Programming and Scripting

Modifying the values of dynamically named arrays

Hi all, In ksh, I'm trying to loop through all of my arrays, named array1, array2, array3..., and update the indices. But I'm getting errors and I'm not sure how to fix them. The errors are ./parse.sh: 6+1: not found The code is: eval \${array$c}=$(eval \${array$c}+1 ) Any help... (12 Replies)
Discussion started by: nicksantos1
12 Replies

3. Shell Programming and Scripting

Using dynamic arrays to extract the values

Hi all, We have requirement to generate load timing based on subject areas HOUSEHOLD, BANKING and TRADING. These values are stored in an array SUB_ARR SUB_ARR=("HOUSEHOLD" "BANKING" "TRADING") Based on indicator files produced while processing data for each type, we need to get the stats (using... (2 Replies)
Discussion started by: sanjaydubey2006
2 Replies

4. Shell Programming and Scripting

How do I find the sum of values from two arrays?

Hi I have redc containing the values 3, 6, 2, 8, and 1. I have work containing the values 8, 2, 11, 7, and 9. Is there a way to find the sum of redc and work? I need to compare the sum of those two arrays to something else, so is it okay to put that into my END? TY! (4 Replies)
Discussion started by: razrnaga
4 Replies

5. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

6. Shell Programming and Scripting

Storing values in arrays

I have the following csh script which lets the use pass the following as an argument -legend=tag1/tag2/tag3/tag4/tag5/tag6/tag7 We pass a number of tags separated by '/'. I want to save the legend tags in an array and have done as below. How can I improve on this as things are getting quite... (3 Replies)
Discussion started by: kristinu
3 Replies

7. Shell Programming and Scripting

Cleaning up Arrays with duplicate values

Hey Gang! So I have two Arrays. @linecount and @hit. Now these arrays contain numbers which I'm using as line placeholders on files. I need these two arrays to sync up and not repeat a number. Here are the contents (spam alert) @linecount 1 28 53 86 87 88 89 90 91 92 93 94 (5 Replies)
Discussion started by: adelsin
5 Replies

8. Shell Programming and Scripting

Storing values in arrays using csh

I am reading a number of files but then I want to put the ranges xmin xmax ymin ymax as arrays for each file. Any idea how I can do this??? set j = 1 echo "Welcome $i times" while ( $j <= $i ) echo "$j" set fname = $fin-bst-misf.xy echo " "$fname ... (0 Replies)
Discussion started by: kristinu
0 Replies

9. Shell Programming and Scripting

storing values in arrays using shell

Friends, I have to execute a command and store its contents into an array using shell. this is what i have tried #!/bin/bash disk_names = ($(`iostat -xtc | egrep -v "device|nfs" | awk '{print $1}'| tr '\n' ' ' `)) But its throwing an error message as ./test-script ./test-script:... (6 Replies)
Discussion started by: achak01
6 Replies

10. UNIX for Dummies Questions & Answers

Concatenating arrays cell values in shell scripting

Hi All, I want to concatenate the array cell values and form a string.. Is it possible? for ex. I have an array word_array contains d u m b and after concatenating the string shld be 'dumb' thanks (2 Replies)
Discussion started by: mathur
2 Replies
Login or Register to Ask a Question