作者laechan (小太保)
看板mud_sanc
标题Re: [问题] 请教各位大神一个sscanf的问题
时间Fri Jun 10 16:55:42 2011
※ 引述《mantohu (StarWars太好看啦!)》之铭言:
: 大家好,小的是乱世的Zoro,一值都有在偷偷的逛ptt偷学,第一次跟大家打招呼 XD
: 话说我最近遇到一个sscanf()的问题想请前辈Wiz们指点~
: 有一个data.o档案,里面有一串mapping是
: all_items (["188931_ONAME":"测试装备",
"188931_UID":"zoro",
: "188931_DATA":(["value":20000,
"limit_con":60,
: "name":"测试装备",
: "armor_prop":(["armor":60,"defense":50,]),
: "id":"test eq",
"limit_str":40,
]),
: "188931_VALUE":1000000,
"188931_TIME":1307688931,
: "188931_FILE":"/obj/temp/test_eq.c",
])
: 也就是前面有一串随机数字加上 _"xxx"当做index,
: 我想以把all_items整串输入进去後
: ,抓取 _ONAME 当关键字然後把前面的随机数字读出来指定该物品
: 下面是这段Code,现在就一值卡在绿色那行,请各位指点一下~
: -----
: int do_list(string arg)
: {
: string msg, name, *items;
: int i, n, j;
: if(!sizeof(all_items)) return 0;
: msg = "";
: items = keys(all_items);
那理论上 items = ({"188931_xx1","188931_xx2",...})
: n = sizeof(items);
: for(i=0;i<n;i++)
: {
: if (sscanf(items, "%d_%s", j, name)==2 && name == "ONAME")
: {
一般来说,如果你很确信 items = keys(all_items) 出来的东西
全都是 数字_字串,那上面可以改写为
sscanf(items,"%d_%s",j,name);
if(name=="ONAME")
{
你可以改写看看。另外我自己很少用 string name,都用 string names
居多,其它像 skills, spells, strs, airs, moneys 等也是, 我的习
惯是很常加 s,原因是不想碰到「保留字」。这你也可以参考。
(像早期的 mudos 版本,「new」并不是保留字,但後来却变保留字)
: if ( (time()-all_items[j+"_TIME"]) < DEADLINE_TIME )
: {
: msg += sprintf(" (%-6d) %-24s %8s %-12s %s\n",
: j,
: filter_color(all_items[items])+"("+all_items[j+"_OID"]+")",
: filter_color(MONEY_D->money_str(all_items[j+"_VALUE"])),
: time_string(86400 - (time()-all_items[j+"_TIME"])),
: all_items[j+"_UNAME"]+"("+all_items[j+"_UID"]+")");
: }
: }
: }
: this_player()->start_more(msg);
: return 1;
: }
: ----
还有另一种改写方法就是用 if(strsrch(items,"_ONAME")>0)
这样的判断方法。
if(strsrch(items,"_ONAME")>0)
{
sscanf(items,"%d_%s",j,name);
Belldandy.
※ 编辑: laechan 来自: 115.82.52.77 (06/10 16:58)
1F:推 justinj :有时後会有保留字....出问题时改一下变数就好了.amem 06/10 17:08
2F:推 mantohu :感谢,我稍後试试看,这是我乱写的拍卖场XD 06/10 17:29
3F:→ laechan :当初光是要改 "new" 这个字改了很久,例如以前更新玩 06/10 17:46
4F:→ laechan :家属性跟3p就是用 new(ppl),後来就改成 new_3p(ppl) 06/10 17:47
5F:→ laechan :呵, 同样都 lpmud 的话就可以参考, 大家相互学习 06/10 17:48