简体   繁体   中英

Erlang arithmetic operations on ets:select result

I have an ets table which you can imagine there are two columns, "Key" and Value. Value is an integer.

When I tried:

 Ans = ets:select(Table_name, MS),
 Ans + 1.

where Ans equals to the expected Value.

I got a error:

 ** exception error: an error occurred when evaluating an arithmetic expression

Can I not do any arithmetic operation on the ets:select return value?

 ets:select(Table_name, MS)

will return result in a List.

So instead, the following would work:

 [Ans] = ets:select(Table_name, MS),
 Ans + 1.

in the ets module spec: select(Tab, MatchSpec) -> [Match] , the reurn value is a list of match. If your match specification define one single integer value as return, you will receive a list of integer. In your case, it seems that this list will always have a length of 1 element, if it must be the case, you can write:

[Ans] = ets:select(Table_name, MS),
Ans + 1.

But beware that this code will crash if the returned list is empty or have more than 1 element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM