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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

gawk mismatch

Status
Not open for further replies.

mslider

Programmer
Joined
Jun 24, 2005
Messages
9
Location
FR
I don't know why but this code below used to perfom crosstab query (see crosstab with AWK question)returns
mismatch (with printing duplicate lines) if I use gawk interpretor instead awk. My gawk version is: GNU Awk 3.1.4.

what's wrong ?

BEGIN {
FS=OFS=";"
}

{
if ( !($2 in cols) ) cols[$2]=++colsN
tag[$1 , $2] = $3
}

END {
# inverting the assoviative array - make it an indexed array
for ( i in cols)
cols[cols] = i

printf("tag%s", OFS)
for(i=1; i<= colsN; i++){
printf("%s%s", cols, (i<colsN) ? OFS : "\n")
}
for( i in tag) {
split(i, iA, SUBSEP)
printf("%s%s", iA[1], OFS)
for(i=1; i <= colsN; i++) {
idx=iA[1] SUBSEP cols
printf("%s%s", (idx in tag) ? tag[idx] : "0", (i<colsN) ? OFS : "\n")
if ( idx in tag) delete tag[idx]
}
}
}
 
it seems gawk is having problem deleting an array cell from within the loop iterating through the same array.

for now use futurelet's code which works correctly under both awk-s.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Have you tried to comment out this line ?
if ( idx in tag) delete tag[idx]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
this is my input file:

AAAAAAAAAA;GSM1;17
AAAAAAATCA;GSM1;1
AAAAAAATTT;GSM1;1
AAAAAACAAA;GSM1;1
AAAAAACTCC;GSM1;1
AAAAAATAAA;GSM1;1
AAAAACAAAA;GSM1;1
AAAAAAAAAA;GSM10419;54
AAAAAAAAAC;GSM10419;2
AAAAAAAAAG;GSM10419;1


with gawk and without comment out this line:
if ( idx in tag) delete tag[idx]

it returns:

tag;GSM1;GSM10419
AAAAAAAAAG;0;1
AAAAACAAAA;1;0
AAAAAAATTT;1;0
AAAAAAAAAA;17;54
AAAAAATAAA;1;0
AAAAAACTCC;1;0
AAAAAACAAA;1;0
AAAAAAAAAA;0;0
AAAAAAAAAC;0;2
AAAAAAATCA;1;0

and now if i comment out the line:

tag;GSM1;GSM10419
AAAAAAAAAG;0;1
AAAAACAAAA;1;0
AAAAAAATTT;1;0
AAAAAAAAAA;17;54
AAAAAATAAA;1;0
AAAAAACTCC;1;0
AAAAAACAAA;1;0
AAAAAAAAAA;17;54
AAAAAAAAAC;0;2
AAAAAAATCA;1;0

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top