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!

re: Awk and Arrays 1

Status
Not open for further replies.

dickiebird

Programmer
Feb 14, 2002
758
GB
Further to previous thread re: Awk and Arrays
How can I load and print arrays when I load it thus :
$1 and $4 are unimportant
$2 is a date eg 23/04/98
$3 is alpha eg "$5 and $6 are numeric eg 4342 3422

DATUM=substr($2,7,2) substr($2,4,2)
VALUM=$5 - (substr($6,6)) + 1
arr[$3,DATUM] += VALUM

then to print :
for ????? in arr
print arr[????????]

I need to group my results thus :
9804 # The date ( though I'd prefer 04/98 )
33434 # Totals for each group
WTCCC 101
CCCEE 2000

9805 # Following month
34432
WTCCC 1002
CCCEE 10

etc
TIA Dickie Bird (:)-)))
 
you would need a nested "for" loop.
Pls post a sample file - the sample from the previous thread doesn't jive with your code. vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hi Vlad
No - it's not the same input file - just similar
(plus a little more)
Here's a sample :
A01000133,24/08/98,A01000219,23/08/98,A01000220,22/08/98,A01000221,23/06/98,A01000222,14/06/98,A01000862,12/05/98,A01000863,04/05/98,A01000981,24/08/98,A01002687,14/08/97,CCCWW,4155,4150,0137
A01004402,12/08/97,CCCWW,3861,3826,0999
A01004416,04/07/97,CCCWW,3861,3853,0999
A01007091,24/08/96,CCCWW,4561,4536,0232
A01007092,14/07/96,CCCWW,4561,4546,0232
A01007093,04/07/96,CCCWW,4561,3556,0232
A01007094,24/04/96,CCCWW,4561,3556,0232

I attempted nesting with a for loop d=0;d<&quot;ZZZZZ&quot;;d++
(looping with an alpha??? )and that's when I came to Tek_tips!
TIA
Dickie Bird (:)-)))
 
something like this:

nawk -f dickie.awk dickie.txt
#------------------------- dickie.awk
BEGIN {
FS=&quot;,&quot;
ARRidGRP=&quot;1&quot;
ARRidDATUM=&quot;2&quot;
}

{
# vlad - old DATUM
#DATUM=substr($2,7,2) substr($2,4,2)
DATUM=substr($2, index($2,&quot;/&quot;)+1);
VALUM=$5 - (substr($6,6)) + 1
arr[$3,DATUM] += VALUM

}

END {
for (iterO in arr) {
split(iterO, outter, SUBSEP);
curDate=outter[ARRidDATUM];
printf(&quot;Date->[%s]\n&quot;, curDate);
for (iterI in arr) {
split(iterI, inner, SUBSEP);
curGrpI=inner[ARRidGRP];
curDateI=inner[ARRidDATUM];
if (curDate == curDateI) {
print &quot;\t&quot; curGrpI, arr[iterI];
delete arr[iterI]
}
}
}
}
vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Vlad - That's brilliant !!!!!
Thanks for your time and effort
I've got to work on the output, to smarten it up a bit
for our fussy users !
What a star - I owe you another beer ! Dickie Bird (:)-)))
 
you're welcome.

NOTE: I'm not sure if you VALUM calculation is the right one given your posted sample file. I'd shy away from using 'substr' whenever I could - using 'substr' makes you more dependent on the specifics of your input stream. vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
OOops - the substr was actually left over from the previous program !
The output groups OK by Type ( etc) but I'd like the dates in chronological order (Currently they appear as they are found in the input file) - Can you show me the way?
TIA Dickie Bird (:)-)))
 
ugly, but I'm kinda busy now - no time to enhance:

#-----------------------------
BEGIN {
FS=&quot;,&quot;
ARRidGRP=&quot;1&quot;
ARRidDATUM=&quot;2&quot;
}

#----------------------------------------------------------------
function cmpDate(d1,d2, d1a,d2a) {
split(d1, d1a, &quot;/&quot;);
split(d2, d2a, &quot;/&quot;);

printf(&quot;Comparing [%s] [%s]\n&quot;, d1, d2);

if ( (d2a[2]+0) > (d1a[2]+0) )
return 1;

if ( (d2a[2]+0) == (d1a[2]+0) && (d2a[1]+0) > (d1a[1]+0))
return 1;

return 0
}

#----------------------------------------------------------------
function isort(A,n, i,j,t) {
for (i = 2; i <= n; i++)
for (j = i; j > 1 && cmpDate(A[j-1],A[j]); j--) {
# swap A[j-1] and A[j]
t = A[j-1]; A[j-1] = A[j]; A[j] = t
}
}

#----------------------------------------------------------------
#----------------------------------------------------------------
{
# vlad - old DATUM
#DATUM=substr($2,7,2) substr($2,4,2)
DATUM=substr($2, index($2,&quot;/&quot;)+1);
VALUM=$5 - (substr($6,6)) + 1
arr[$3,DATUM] += VALUM
if ( !(DATUM in dateARR)) {
dateARRnum++;
dateARR[DATUM]++;
}
}

END {
# inverting the assoviative array - make it an indexed array
for ( i in dateARR) {
dateARRinv[++j]=i;
delete dateARR;
}

isort(dateARRinv, dateARRnum);
for(i=1; i <= dateARRnum; i++) {
datePivot=dateARRinv;
for (iterO in arr) {
split(iterO, outter, SUBSEP);
curDate=outter[ARRidDATUM];
if ( curDate != datePivot)
continue;
printf(&quot;Date->[%s]\n&quot;, curDate);
for (iterI in arr) {
split(iterI, inner, SUBSEP);
curGrpI=inner[ARRidGRP];
curDateI=inner[ARRidDATUM];
if (curDate == curDateI) {
print &quot;\t&quot; curGrpI, arr[iterI];
delete arr[iterI]
}
}
}
}
}
vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks again, Vlad - where would this forum be without you ?!
Dickie Bird (:)-)))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top