Re: [問題] 請教各位大神一個sscanf的問題
看板mud_sanc (Sanctuary - 聖殿)作者laechan (小太保)時間14年前 (2011/06/10 16:55)推噓2(2推 0噓 3→)留言5則, 3人參與討論串2/2 (看更多)
※ 引述《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)
推
06/10 17:08, , 1F
06/10 17:08, 1F
推
06/10 17:29, , 2F
06/10 17:29, 2F
→
06/10 17:46, , 3F
06/10 17:46, 3F
→
06/10 17:47, , 4F
06/10 17:47, 4F
→
06/10 17:48, , 5F
06/10 17:48, 5F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):
mud_sanc 近期熱門文章
PTT遊戲區 即時熱門文章
15
19