tags:

views:

90

answers:

2

I've been using awk to process hourly weather data, storing 10 arrays with as much as 8784 data elements. If an array is incomplete, i.e., stops at 8250, etc., after the "END" command I fill the remaining array elements with the last available value for the array. However, when I then print out the complete arrays, I get 0's for the filled values instead. What's causing this?? Does awk have a limit in the array size that's preventing it from filling the arrays? Following is a snippet of the awk program. In the two print statements, the first time the array elements are filled, but the second time they're empty.

Any help is appreciated because this problem is holding up my work.

Joe Huang

END{
if (lastpresstime < tothrs)
   {
   diffhr = tothrs - lastpresstime
   for (i=lastpresstime+1;i<=tothrs+1;i++)
      {
      xpressinter[i]=diffhr
      xpressrecords[i]=diffhr
      xipress[i]=lastpress
      xpressflag[i]="R"
      printf("PRS xipress[%4d] =%6.1f\n",i,xipress[i]) > "ncdcfm3.prs"
      printf(" xipress[%4d] =%6.1f%1s\n",i,xipress[i],xpressflag[i])
      }
   for (i=1;i<=tothrs+1;i++) printf("PRS xipress[%4d] =%6.1f\n",i,xipress[i]) 
   }

~

A: 

As 0 is the default value for an unknown/unused numerical variable, I would ask if you are sure, that there is no mistype in the variable names used in the END block?

Juergen Hartelt
+1  A: 

I don't have the rep to edit your post, but here's the formatted code:

END {
    if (lastpresstime < tothrs) {
        diffhr = tothrs - lastpresstime
        for (i=lastpresstime+1;i<=tothrs+1;i++) {
            xpressinter[i]=diffhr
            xpressrecords[i]=diffhr
            xipress[i]=lastpress
            xpressflag[i]="R"
            printf("PRS xipress[%4d] =%6.1f\n",i,xipress[i]) > "ncdcfm3.prs"
            printf(" xipress[%4d] =%6.1f%1s\n",i,xipress[i],xpressflag[i])
        }
        for (i=1;i<=tothrs+1;i++)
            printf("PRS xipress[%4d] =%6.1f\n",i,xipress[i])
    }
}

Note that I added a matching brace at the end.

I don't see any inherent problems in the code, so like jhartelt, I have to ask - are all of the variables properly defined? We can't tell from this sample how lastpresstime, tothrs, and lastpress get their values. In particular, if lastpress isn't, you'll get exactly the behavior you described. Note that if you have misspelled it, it will be an undefined variable and therefore use the default value of 0.

With respect to William Pursell's comment, there should also be no difference in the output of xipress[i] between the three printfs (for lastpresstime<i).

Jefromi