Question: How to sum entries in matrix based on key values?

For learning, I was trying to implement this in Maple. Given this matrix

m:=<<"foobar"   | 77>,
         <"faabar"   | 81>,
         <"foobaa"  | 22>,
         <"faabaa"  | 8>,
         <"faabian"  |88>,
         <"foobar"  | 27>,
         <"fiijii"      | 52>>;

And list of keys 

keys:={"foo","faa"}:

The idea to find the entries (in first column) which starts with the keys, and sum the corresponding numerical value in the second column. The result will be

r:= <<"foo"|126>,
        <"faa"|177>>

I tried using select, but select, once something is found, does not allow one to do anything more, so it is not very useful. For example  select[flatten](x->x[1..3]="foo",m);  just returns the fields in first column. 

I can extract rows I want like this

f:=x->seq(`if`(m[i,1][1..3]=x,m[i],NULL),i=1..7):
r:=map(f,keys);

Not very useful, but at least I got the parts I want, but still need to process these again.

I could write a loop to do the whole thing, but I am trying to avoid this.

What would be the correct Maple way to do this? I think there might be a command in some package that will do this in one or 2 lines only.

 

Please Wait...