<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Hi Yuan,</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre"> </span>I have proposed two alternatives <a href="http://tryerl.seriyps.ru/#id=2af3" class="">here</a>. ‘brujo1’ assumes you don’t care about the order of the companies in your company_list. ‘brujo2’ is entirely compatible with your function.</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre"> </span>Cheers!</div><br class=""><div><blockquote type="cite" class=""><div class="">On Nov 23, 2015, at 07:06, YuanZhiqian <<a href="mailto:on-your-mark@hotmail.com" class="">on-your-mark@hotmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" style="font-family: 微软雅黑; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Hi Guys,<div class=""><br class=""></div><div class=""> Sorry to bother you again... I am totally a green hand in Erlang :(</div><div class=""><br class=""></div><div class=""> 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 class=""><br class=""></div><div class=""> I have a list of records, whose definition is as follows</div><div class=""><br class=""></div><div class=""><div class="">-record(company_info, {</div><div class=""> company_id,</div><div class=""> budget,</div><div class=""> consumption,</div><div class=""> compaign_ids</div><div class=""> }).</div></div><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">///////////////////////////////////In C&C++///////////</div><div class="">CompanyInfo co_list[1000];</div><div class="">/* initialized somewhere else ... */</div><div class=""><br class=""></div><div class="">for (size_t i = 0; i < 1000; ++i) {</div><div class=""> if (co_list[i].comany_id == co_id) {</div><div class=""> <span class="Apple-converted-space"> </span><b class=""><font color="#ac193d" class="">co_list[i].consumption += price;<span class="Apple-converted-space"> </span></font> </b></div><div class=""> break;</div><div class=""> }</div><div class="">}</div><div class=""><br class=""></div><div class="">if (i == 1000)</div><div class=""> err_msg = "bla...";</div><div class="">//////////////////////////////////////////////////////</div><div class=""><br class=""></div><div class="">But in Erlang, I couldn't find a straightforward way to do this, my code is like this:</div><div class=""><br class=""></div><div class="">////////////////////////////In Erlang//////////////////////////</div><div class="">%Co_list is the list of company_info records</div><div class=""><br class=""></div><div class=""><div class="">cal_win_notice({Co_id, Ca, Adgrp, Price, Cur}, </div><div class=""> #state{company_list = Co_list, campaign_list= Ca_list} = State) -></div><div class=""> case lists:any(fun(#company_info{company_id = A}) -> A == Co_id end, </div><div class=""> Co_list) of</div><div class=""> true -> </div><div class=""><b class=""><font color="#ac193d" class=""> New_co_list = lists:map(fun(R) -> </font></b></div><div class=""><b class=""><font color="#ac193d" class=""> case R#company_info.company_id of</font></b></div><div class=""><b class=""><font color="#ac193d" class=""> Co_id -></font></b></div><div class=""><b class=""><font color="#ac193d" class=""> R#company_info{consumption = R#company_info.consumption + Price};</font></b></div><div class=""><b class=""><font color="#ac193d" class=""> _ -></font></b></div><div class=""><b class=""><font color="#ac193d" class=""> R</font></b></div><div class=""><b class=""><font color="#ac193d" class=""> end</font></b></div><div class=""><b class=""><font color="#ac193d" class=""> end,</font></b></div><div class=""><b class=""><font color="#ac193d" class=""> Co_list),</font></b></div><div class=""><b class=""><font color="#ac193d" class=""> {ok, State#state{company_list = New_co_list}};</font></b></div><div class=""> false -></div><div class=""> {not_found, State} </div><div class=""> end.</div></div><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">I would appreciate so much with any advises.</div><div class=""><br class=""></div><div class="">Best regards</div><div class="">Zhiqian</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div></div><span style="font-family: 微软雅黑; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">_______________________________________________</span><br style="font-family: 微软雅黑; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: 微软雅黑; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">erlang-questions mailing list</span><br style="font-family: 微软雅黑; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="mailto:erlang-questions@erlang.org" style="font-family: 微软雅黑; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">erlang-questions@erlang.org</a><br style="font-family: 微软雅黑; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="http://erlang.org/mailman/listinfo/erlang-questions" style="font-family: 微软雅黑; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">http://erlang.org/mailman/listinfo/erlang-questions</a></div></blockquote></div><br class=""></body></html>