Subj : Re: any function to handle this kind of counting? To : comp.programming From : cri Date : Sat Jul 30 2005 09:50 pm On Fri, 29 Jul 2005 23:30:10 +0800, "Ross" wrote: >Dear all, [snip] >and then i want to count, say, for "1", there are two A's, one E, ... > >it seems that a for loop can do the job but as we don't know how many >instances there are, i.e. don't know A, C, E and then what will occur in >this corresponding vector, is there any function which is devoted to handle >this kind of counting problem? thx!! You didn't specify what language you mean to use (though I suspect it's something from the C family) so you've opened the flood gates to the usual suspects puffing off their favorite language du jour. (Nothing like mixing a few metaphors.) Just to join the fun, here the code in San, a language I am developing. (Not yet available, sorry.) paired[].a = a[]; paired[].b = b[] begin foreach pair in paired[] count[pair.a].list.{pair.b} = @+1 end begin foreach val in count[] emit val ":" [val.list.[] | emit "(" %1'name ": " %1 ")" ] emit \n end I presume that the code is suitably cryptic for those not familiar with the syntax. More to the point the general answer to your question is that few languages, perhaps none, would have a specific function that meets your particular need. That said, there are several ways that one can do this. One is to build an array of lists (that's what the above code does), another is to build a matrix with rows being integers, the columns being letters, and the cells containing counts. One can treat the two vectors as paired elements and first sort on letter, then on integer. You can put the whole thing in a tree with nodes containing counts. USW. Richard Harter, cri@tiac.net http://home.tiac.net/~cri, http://www.varinoma.com Save the Earth now!! It's the only planet with chocolate. .