Problem with arrays in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with arrays in awk
# 1  
Old 05-10-2008
Problem with arrays in awk

Hello! I'm trying to make a script that will make a list of the files in a source tree and sort them by size. Problem is I've run into a weird problem.
Code:
print array[index]

will give me numbers like 160, 220, 444 that i don't even know where they come from, and
Code:
print array[index+1]

will give me the correct numbers !?? I'm puzzled. Thanks in advance!
Code:
du `find ./ -name *.c*` | awk '{ line[NR] = $0 
				 size[NR] = $1}
				    END { sort(line, size, NR)
					  for(i = 1; i < NR; i++) print line[i] }
				    function sort(A, S, Q,       Y, R, TEMP) {
					for(Y = 1; Y < Q; Y++){						
						for(R = 1; R < Q+1; R++) {
							if( S[Y] > S[Y+1] ) {
								print "TRUE: "S[Y] " is larger than " S[Y+1] "."
								print "S[" Y "] is " S[Y] " even though S[" (Y+1) "] is " S[Y+1] "."
								TEMP = S[Y]
								S[Y] = S[Y+1]
								S[Y+1]=TEMP
								TEMP = A[Y]
								A[Y] = A[Y+1]
								A[Y+1] = TEMP
							}
						}
					}
				    }' > sources

# 2  
Old 05-11-2008
I didn't check the specificity of your scripts but be aware that the way awk stores array indexes is a bit peculiar. Check this:
Code:
awk 'BEGIN{
   arr[0444]="foo"
   arr[292]="bar"
   print arr[0444] # returns ---> bar
}


This is because awk will convert integers or decimal to string and octal to integer to string. If your script enters 0444 as index, awk will see it as an octal. Awk will first convert it to a integer (decimal base) and only then into a string.

0444 (octal) == 292 (decimal)

May be this has nothing to see with your problem but check. You never know. I just came across this one in one of my scripts and it took me some time to find the reason.

Last edited by ripat; 05-11-2008 at 04:54 AM..
# 3  
Old 05-11-2008
Isn't much more simple using the sort command instead of building your custom sorting function in awk?

Code:
du `find ./ -name "*.c*"` | sort -n

# 4  
Old 05-11-2008
Thanks a lot for both replies! Yes using sort is much simpler, faster, and I feel stupid. Smilie
Still, I'm confused with that behavior... ripat must be right in that it must have to do with the conversion to string. I even tried changing CONVFMT, but with no success.
Thanks anyway.
Best regards, Glauco.
# 5  
Old 05-12-2008
I tried to reproduce the problem with your code, but couldn't.

By the way, I think your sort algorithm was incomplete anyway, right? You never used the R indices.
# 6  
Old 05-12-2008
Actually, I made quite a lot of mistakes:
1) I inverted the two for lines inside the sort function: the loop with the Y index should be inside the one with the R.
2) I forgot to protect the expression passed to find
3) i < NR would prevent the last record from being printed...
4) sort would be a much simpler solution

Got it working now! Thanks.
Code:
du `find ./ -regex '.*.c\(pp\)?'` | awk '{ line[NR] = $0 
				 size[NR] = $1}
				    END { sort(line, size, NR)
					  for(i = 1; i <= NR; i++) print line[i] }
				    function sort(A, S, Q,       Y, R, TEMP) {
					for(R = 1; R < Q; R++){						
						for(Y = 1; Y < Q; Y++) {
							if( S[Y] > S[Y+1] ) {
								#print "TRUE: "S[Y] " is larger than " S[Y+1] "."
								#print "S[" Y "] is " S[Y] " even though S[" (Y+1) "] is " S[Y+1] "."
								TEMP = S[Y]
								S[Y] = S[Y+1]
								S[Y+1]=TEMP
								TEMP = A[Y]
								A[Y] = A[Y+1]
								A[Y+1] = TEMP
							}
						}
					}
				    }' > sources

Or the boring simple solution: Smilie
Code:
du `find ./ -regex '.*.c\(pp\)?'` | sort -n > sources

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Arrays in awk

Array A contains lines of numbers from files. Array B contains number of fields for each line of each file. I want to run on array A. To do that I need to know the number of fields for each file in array A (because each line doesn't have the same NF). The NF is in array B, problem is: I don't... (6 Replies)
Discussion started by: guitarist684
6 Replies

2. Shell Programming and Scripting

awk Arrays

So I'm back once again beating my head off a wall trying to figure out how to get this to work. My end goal is to take input such as what's below, which will be capture in real time with a tail -f from a file or piped output from another command: ... (5 Replies)
Discussion started by: ShadowBlade72
5 Replies

3. Programming

problem in multiplying arrays

Hi, this is my code.It's simple : there are 2 2D arrays and the multiplied to C. #include<stdio.h> #include<sys/shm.h> #include<sys/stat.h> #include<stdlib.h> main() { int *A; //A int *B; //B int *C; //C int i,j,x,k,d; int id; ... (17 Replies)
Discussion started by: giampoul
17 Replies

4. Shell Programming and Scripting

Problem with arrays and loop

Hello , im sorry for my english . im trying to create a dynamic menu that will display if the interface is ACTIVE OR STOPPED/FAILED for some reason i cant get it to work properly start_interface_func() { i=0 for interface_chk in 11 71 73 72 12 47 48 49 50 20 23 24 25 46 21 22 27 28... (5 Replies)
Discussion started by: visiown
5 Replies

5. Shell Programming and Scripting

Problem with arrays

Hi I have two arrays: arr1 = (demo demo2 demo3 demo4 demo5) arr2 = (demo2 test demo) I want to check that the values the "arr2" are present in "arr1" Example arr1 = (demo demo2 demo3 demo4 demo5) arr2 = (demo2 test demo) Output: Error arr1 = (demo demo2 demo3 demo4 demo5)... (3 Replies)
Discussion started by: blito_loco
3 Replies

6. UNIX for Dummies Questions & Answers

Problem assigning variables to arrays

Hi All, I have a problem assigning variables to script.I have a script in which i have a while loop now i have to assign some values obtained to an array which will be used later in the script.Can anyone help how to do that. At present my scrot looks like: co=0 pco=0 co=`cat /tmp/highcpu... (4 Replies)
Discussion started by: usha rao
4 Replies

7. Shell Programming and Scripting

awk arrays can do this better - but how?

Hi, I have spent the afternoon trawling Google, Unix.com and Unix in a Nutshell for information on how awk arrays work, and I'm not really getting too far. I ahve a batch of code that I am pretty sure can be better managed using awk, but I'm not sure how to use awk arrays to do what I'm... (1 Reply)
Discussion started by: littleIdiot
1 Replies

8. Shell Programming and Scripting

Arrays in awk

Hi, I've written the following code to manipulate the first 40 lines of a data file into my desired order: #!/bin/awk -f { if (NR<=(4)){ a=a$0" "} else { if ((NR >= (5)) && (NR <= (13))) { b=b$0" " } else {if ((NR >= (14)) && (NR <= (25))){ c=c$0" "} ... (5 Replies)
Discussion started by: catwoman
5 Replies

9. Programming

C programming + problem with char arrays

Im trying to write some code atm which gets the complete pathname of a folder and strips off references to the parent folders. The end result should be just the name of the folder. Currently Im able to extract the folder name, however Im getting junk added onto the name as well which is making... (7 Replies)
Discussion started by: JamesGoh
7 Replies

10. Shell Programming and Scripting

Need Help with awk and arrays

now its owkring - thanks fo rthe help all . (7 Replies)
Discussion started by: fusionX
7 Replies
Login or Register to Ask a Question