Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Awk and Arrays/Structures 1

Status
Not open for further replies.

dickiebird

Programmer
Feb 14, 2002
758
GB
Hi Guys
I have a long-winded Awk script which I now need to enhance.
I need to create sub-totals from a file for several date ranges (eg Pre March 1999,March 1999 to Sept 1999, Sept 1999 to March 2000 etc etc)
I have 5 different values to total for each date range :
File sample: (3 of 2million+ rows)

E08308311,8330,WWTV,GBP,50,I,05/02/01,03/02/01,1069,24/08/01
E08308331,8350,WWTV,GBP,5,I,27/03/01,24/03/01,0866,0,
E08308351,8355,WWTV,GBP,10,I,09/01/01,05/01/01,0310,11/07/01

Will Awk allow arrays to which I can refer ?
Perhaps such as : TotArr [][] ??????

where TotArr[0][0] holds my running total for Pre March 1999 £5 values
and TotArr[0][1] holds my running total for Pre March 1999 £10 values
and TotArr[0][2] holds my running total for Pre March 1999 £50 values
etc etc
and TotArr[8][0] holds my running total for March 2002 £5 values
and TotArr[8][1] holds my running total for March 2002 £10 values
etc etc

And how do I initialize it at the start ?
Thanks in advance Dickie Bird (:)-)))
 
something like that:
assuming:
$5 - dollar/pound amount
$8 - date span (not sure if that's the one you wanna use)
$9 - total

arr[$5,$8] = $9

Is that something you're looking for?
vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Sorry Vlad - I haven't made it very clear, have I ?
I need to sub-total all values in column 9 ($9) depending on the denomination in $5 (5,10,50 etc) into an area for each denomination, in the date ranges. I've got 9 date ranges (6 monthly, for past 5 years) and could do it like:
if ($9=5 && $7 'in date range') d5m1=d5m1 + $9
if ($9=50 && $7 'in date range') d50m1=d50m1 + $9
etc etc for all 9 ranges for all 5 denominations
Then I need to print the 5 x 9 totals at END
I wondered if Awk could allow me to use a 2D array somehow.
TIA Dickie Bird (:)-)))
 
yeah, I think I understand.
You can use 2D arrays that are associative in nature.

Your first dimension is dollar/pound denomination: 5, 10, 50 etc

Your second dimention is the date range - I assume you know how to figure out if a given date falls within a well-known range.

The value of the array entry [cell] is your sub-total:

arr[$5, <yourDateRange>] += $9

Is it gettin' warmer? ;) vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Once again I'm in your debt !
Excellent response - All working brilliantly !
Thanks Vlad
(I may be back ! ) Dickie Bird (:)-)))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top