<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:΢ÈíÑźÚ
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>Hi Guys,<div><br></div><div> Sorry to bother you again... I am totally a green hand in Erlang :(</div><div><br></div><div> Here is the problem I am now struggling with: in C&C++, we are always able to update a field in an object simply using obj.field1 = Value; But in Erlang, since its once-bound-never-change policy, people would have to find a way around to achieve the same goal, and things are getting worse when the data structure is a little more complicated, which is my case:</div><div><br></div><div> I have a list of records, whose definition is as follows</div><div><br></div><div><div>-record(company_info, {</div><div> company_id,</div><div> budget,</div><div> consumption,</div><div> compaign_ids</div><div> }).</div></div><div><br></div><div>What I want to do is to add a value to the consumption of the record whose company_id is given, and returning error message if there's no such record found. Written in C, that would be:</div><div><br></div><div>///////////////////////////////////In C&C++///////////</div><div>CompanyInfo co_list[1000];</div><div>/* initialized somewhere else ... */</div><div><br></div><div>for (size_t i = 0; i < 1000; ++i) {</div><div> if (co_list[i].comany_id == co_id) {</div><div> <b><font color="#ac193d">co_list[i].consumption += price; </font> </b></div><div> break;</div><div> }</div><div>}</div><div><br></div><div>if (i == 1000)</div><div> err_msg = "bla...";</div><div>//////////////////////////////////////////////////////</div><div><br></div><div>But in Erlang, I couldn't find a straightforward way to do this, my code is like this:</div><div><br></div><div>////////////////////////////In Erlang//////////////////////////</div><div>%Co_list is the list of company_info records</div><div><br></div><div><div>cal_win_notice({Co_id, Ca, Adgrp, Price, Cur}, </div><div> #state{company_list = Co_list, campaign_list= Ca_list} = State) -></div><div> case lists:any(fun(#company_info{company_id = A}) -> A == Co_id end, </div><div> Co_list) of</div><div> true -> </div><div><b><font color="#ac193d"> New_co_list = lists:map(fun(R) -> </font></b></div><div><b><font color="#ac193d"> case R#company_info.company_id of</font></b></div><div><b><font color="#ac193d"> Co_id -></font></b></div><div><b><font color="#ac193d"> R#company_info{consumption = R#company_info.consumption + Price};</font></b></div><div><b><font color="#ac193d"> _ -></font></b></div><div><b><font color="#ac193d"> R</font></b></div><div><b><font color="#ac193d"> end</font></b></div><div><b><font color="#ac193d"> end,</font></b></div><div><b><font color="#ac193d"> Co_list),</font></b></div><div><b><font color="#ac193d"> {ok, State#state{company_list = New_co_list}};</font></b></div><div> false -></div><div> {not_found, State} </div><div> end.</div></div><div><br></div><div>Well, as shown above, the codes in bold fonts are doing the same logic which in C can be done in just one expression. This will certainly make the code unnecessarily verbose when there are more similar operations to come. What should I do? I think this is due to Erlang's feature, but in case I am wrong and making a simple thing complicated ...</div><div><br></div><div>I would appreciate so much with any advises.</div><div><br></div><div>Best regards</div><div>Zhiqian</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div> </div></body>
</html>