sorting data using array in ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sorting data using array in ksh
# 1  
Old 12-04-2007
sorting data using array in ksh

plz help me..........i have a ksh script that sorts data in ascending order.
the 1st half is correct,but for the line no 31 its showing problem

1 #!/bin/ksh
2
3
4
5 echo "Enter the array length"
6 read num
7
8
9 echo "enter the elements"
10
11 i=0
12
13 while (( i < num ))
14 do
15 read element1[i]
16 i=`expr $i + 1`
17 done
18
19 i=0
20 echo "The elements are"
21 echo ${element1[*]}
22
23 i=0
24 temp=0
25 while (( i < num ))
26 do
27
28 j=`expr $i + 1`
29 while (( j < num ))
30 do
31 if (( element1[i] > element1[j] ))
32 then
33 temp=$element1[i]
34 element1[i]=$element1[j]
35 element1[j]=$temp
36 else
37 echo " "
38 fi
39 j=`expr $j + 1`
40 done
41 i=`expr $i + 1`
42 done
43
44 echo "The element in ascending order are "
45 echo ${element1[*]}
----------------------------------------------------------------------
# 2  
Old 12-04-2007
Hi,

The problem in the above code is in accessing the array elements.

The array elements should be accessed as ${array[num]}

Use the below code in your script,

if (( ${element1[i]} > ${element1[j]} ))
then
temp=${element1[i]}
element1[i]=${element1[j]}

Regards,
Chella
# 3  
Old 12-04-2007
thanks a lot dear.got it

very much thanks to u.
hey can u also tell me how to blink a text in a file and contnue with the rest of the program.
actually it only blinks and i cannot do the rest as i should wait for the blinking to stop

can u show me a simple example in ksh
# 4  
Old 12-04-2007
Ordinarily, yes, you would need to quote subscripted variables within braces "{ }" to protect the square brackets "[ ]" from the shell when accessing them. (You don't need to do this when setting them.) This should not be necessary inside the evaluation of double-parenthesis "(( ))". So the problem is not at line 31, but at 33, 34, and 35, using the syntax as suggested above. It just shouldn't be needed in the "if" clause.

You can blink text with "tput" if your terminal type supports blinking text. See the man page of tput for the correct options to use (i.e., I can't remember!)
# 5  
Old 12-04-2007
thanks dear for ur suggestion !

i have tried that but not able to blink the text simultaneously and proceed with the prorgam
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting output of AWK array

I need help to sort the output of an awk array Example datadata="1 blue 2 green 3 blue 4 yellow 5 blue 6 red 7 yellow 8 red 9 yellow 10 yellow 11 green 12 orange 13 black" My awk line to get output in one lineecho "$data" | awk ' {arr++; next} END { for (i in arr) { if(arr>1 )... (2 Replies)
Discussion started by: Jotne
2 Replies

2. Shell Programming and Scripting

Sorting awk array output?

Hi all, I have a script which produces a nice table but I want to sort it on column 3. This is the output line in the script: # Output { FS = ":"; format = "%11s %6s %-16s\n"; prinft "\n" printf ( format, "Size","Count","Who" ) } for (i in... (21 Replies)
Discussion started by: Cowardly
21 Replies

3. Shell Programming and Scripting

[Perl] Sorting an String-Array

Hi, i have a txtfile with the format <Nr>tab<word>tab<other stuff>new line and i want to sort the <word>-colum with a perl script. My textfile: <Nr>tab<word>tab<other stuff>new line 6807 die ART.Acc.Sg.Fem 6426 der ART.Gen.Sg.Fem 2 die ART.Nom.Sg.Fem 87 auf APPR.-- 486 nicht PTKNEG.--... (1 Reply)
Discussion started by: buckelede
1 Replies

4. Shell Programming and Scripting

sorting multi dimensional array

Hi there, Can someone let me know how to sort the 2 dimensional array below by column 1 then by column 2? 22 55 2222 2230 33 66 44 58 222 240 11 25 22 60 33 45 output: 11 25 22 55 22 60 33 45 33 66 44 58 (6 Replies)
Discussion started by: phoeberunner
6 Replies

5. Shell Programming and Scripting

sorting numeric array

Hi, I would like to do the following sorting, but the output is not what i expected. Why 222 and 2222 are not at the last two elements of array? awk 'BEGIN{a="22";a="2222";a="33";a="44";a="222";a="11";a="22";a="33";asort(a); for (i=1;i<=8;i++) print a}' 11 22 22 222 2222 33 33 44... (1 Reply)
Discussion started by: phoeberunner
1 Replies

6. Shell Programming and Scripting

perl array sorting

Hi All, I have an array in perl as @match = (201001,201002,201001,201002); I am trying to sort this array as @match = sort(@match); print "@match"; I dont see the output sorted any answers I also tried another way, but still the results are not sorted foreach my $match (sort { $a... (2 Replies)
Discussion started by: bsdeepu
2 Replies

7. Shell Programming and Scripting

Sorting value frequency within an array

How is it possible to sort different nummeric values within an Array. But i don`t want the highest or the lowest. I need the most frequently occurring value. For examble: My Array has to following values = (200 404 404 500 404 404 404 200 404) The result should be 404 The values are... (3 Replies)
Discussion started by: 2retti
3 Replies

8. Shell Programming and Scripting

Sorting Awk generalized array

Generalized arrays take any type of variable(s) as subscripts, but the subscript(s) are treated as one long string expression. The use of for(a in x) on a generalized array will return all of the valid subscripts in some order, not necessarily the one you wished. How can I make it so that i... (2 Replies)
Discussion started by: gio001
2 Replies

9. Shell Programming and Scripting

Sorting problem (SunOS 5.9 / KSH).

Hi, Can someone tell me why the "LargeFile" is coming first before the smaller files. Is there any way to list the files based on size column. ls -g| sort -k 4 -rw-r--r-- 1 user 6117910528 Apr 28 15:04 LargeFile -rw-r--r-- 1 user 6136832 May 30 07:23 my_20080530.tar -rw-r--r-- ... (2 Replies)
Discussion started by: kesari
2 Replies

10. Shell Programming and Scripting

Perl: Sorting an associative array

Hi, When using sort on an associative array: foreach $key (sort(keys(%opalfabet))){ $value = $opalfabet{$key}; $result .= $value; } How does it handle double values? It seems to me that it removes them, is that true? If so, is there a way to get... (2 Replies)
Discussion started by: tine
2 Replies
Login or Register to Ask a Question