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

incrementing blank array element

Status
Not open for further replies.

cback

Programmer
May 30, 2002
3
GB

Hi

Probably an easy one but here goes;

I want to increment a count in an array like this:-
arr[$1]++
to count occurences of a value through a file. But if that element of the array is NULL or blank this doesn't work. Is there a simpler one liner to initialise and then increment the element better than:-
if (arr[$1]=="") {arr[$1]=0};
arr[$1]++;

Thanks

Chris
 
Hi cback-

I have tried to solve this for you, but alas, no dice!

However, to understand what is happening, refer to the AWK SUMMARY in APPENDIX A (page 192) of The AWK Programming Language book. Here you will find a lucid description as to why AWK acts this way.

HTH







flogrr
flogr@yahoo.com

 
You can't access an array with an index of nil. Nothing is as ambiguos as infinity. But why not save the count to a separate variable. i.e.

($1 != "" ? arr[$1]++ : NULLCT++)

Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top