Garbage Collection

塵も積もれば山

目次

Blog 利用状況

ニュース

C++とかC#とか数学ネタを投下していく予定です。

[その他のページ]
日々の四方山話を綴った日記出水の日記帳

書庫

日記カテゴリ

[C][Lua]テリブルテーブル

[C][Lua]あたいが欲しいの続きです。

今度はこういう風なテーブルのデータを取る方法です。

apple = {
  price = 100,
  categoly = "fruit",
  color = "red",
}

このようなテーブルを取るには、まずlua_getglobal()でテーブルをスタックに積んだ後、
そのテーブルを指定してlua_getfield()を使います。
このとき注意したいのが、lua_getfield()は値をスタックにおきますが、
スタックからなにも取り除かないことです。
したがって、テーブルとテーブルの値と2つがスタックに残っています。

lua_getglobal(L, "apple");
lua_getfield(L, -1, "price");
int price = lua_tointeger(L, -1);
lua_pop(L, 2);

逆に、テーブルに値を入れるにはlua_setfield()を使います。
入れるために使った値はスタックから取り除かれますが、
テーブルそのものは残っています。

lua_getglobal(L, "apple");
lua_pushinteger(L, 120);
lua_setfield(L, -2, "price");
int price = lua_tointeger(L, -1);
lua_pop(L, 1);

ただ、lua_getfield, lua_setfieldは連想配列にしか使えません。
添え字に数値を取る配列にはlua_gettable, lua_settableを使います。

// このようなLuaを読んだ後とする
// num = {"one", "two", "three"}

lua_getglobal(L, "num");
lua_pushinteger(L, 2);
lua_getfield(L, -2);
printf("%s\n, lua_tostring(L, -1));
lua_pop(L, 2);

// このプログラムをLuaで書くと以下のとおりになる
// print num[2]
// 実行結果はtwo

lua_getglobal(L, "num");
lua_pushinteger(L, 2);
lua_pushstring(L, "zwei");
lua_settable(L, -3);
lua_pop(L, 1);

// num[2] = "zwei"

lua_gettableはキーを取り除いて値を積むので、スタックの増減は0、
lua_settableはキーも値も取り除くのでスタックの増減は-2です。
どちらにしろ、テーブルは残ることに注意してください。

最後、テーブルの全要素を取る方法です。

lua_getglobal(L, "all");
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
  lua_pushvalue(L, -2);
  printf("%s, %s\n", lua_tostring(L, -1), lua_tostring(L, -2));
  lua_pop(L, 2);
}
lua_pop(L, 1);

これは、allというテーブルの全要素のキーと値を表示するプログラムです。

lua_tostringはスタックの文字列を取る関数ですが、これを実行したとき、
スタックの中身が数値だと、スタックの内容を文字列に変換してしまいます。
これではまずいので、lua_pushvalueを使ってコピーを作ったあと、
コピーに対してlua_tostringをかけることで問題を回避しています。

lua_nextが、テーブルとキーを見て、次のキーを取ってくる関数なので、
キーを数値から文字列に変換してしまうことがよくないわけです。
ですから、値の方はコピーせずそのまま使っています。

投稿日時 : 2009年3月27日 15:45

Feedback

# fvJASPKyBhbq 2012/01/07 7:31 http://www.luckyvitamin.com/c-204-red-yeast-rice

Yeah, it is clear now !... From the very beginning I did not understand where was the connection with the title !!...

# Do you have any video of that? I'd want to find out more details. 2019/05/05 22:49 Do you have any video of that? I'd want to find o

Do you have any video of that? I'd want to find out more details.

# We stumbled over here different website and thought I might as well check things out. I like what I see so now i am following you. Look forward to exploring your web page repeatedly. 2019/07/09 15:15 We stumbled over here different website and thoug

We stumbled over here different website and thought I might as well check things out.

I like what I see so now i am following you. Look forward
to exploring your web page repeatedly.

# Yes! Finally someone writes about how to get help in windows 10. 2019/07/18 8:57 Yes! Finally someone writes about how to get help

Yes! Finally someone writes about how to get help in windows 10.

# Yes! Finally someone writes about how to get help in windows 10. 2019/07/18 8:58 Yes! Finally someone writes about how to get help

Yes! Finally someone writes about how to get help in windows 10.

# Hi Dear, are you in fact visiting this site regularly, if so after that you will without doubt take pleasant knowledge. 2019/07/21 19:42 Hi Dear, are you in fact visiting this site regula

Hi Dear, are you in fact visiting this site regularly,
if so after that you will without doubt take pleasant knowledge.

# I couldn't resist commenting. Exceptionally well written! 2019/08/14 7:45 I couldn't resist commenting. Exceptionally well w

I couldn't resist commenting. Exceptionally well written!

# What a information of un-ambiguity and preserveness of precious experience regarding unexpected feelings. 2019/09/02 2:50 What a information of un-ambiguity and preservenes

What a information of un-ambiguity and preserveness of precious experience regarding unexpected feelings.

# Howdy just wanted to give you a brief heads up and let you know a few of the pictures aren't loading correctly. I'm not sure why but I think its a linking issue. I've tried it in two different web browsers and both show the same outcome. 2022/03/24 6:49 Howdy just wanted to give you a brief heads up and

Howdy just wanted to give you a brief heads up and let you know a few of the pictures aren't loading correctly.

I'm not sure why but I think its a linking issue.

I've tried it in two different web browsers and
both show the same outcome.

# Howdy just wanted to give you a brief heads up and let you know a few of the pictures aren't loading correctly. I'm not sure why but I think its a linking issue. I've tried it in two different web browsers and both show the same outcome. 2022/03/24 6:51 Howdy just wanted to give you a brief heads up and

Howdy just wanted to give you a brief heads up and let you know a few of the pictures aren't loading correctly.

I'm not sure why but I think its a linking issue.

I've tried it in two different web browsers and
both show the same outcome.

# Howdy just wanted to give you a brief heads up and let you know a few of the pictures aren't loading correctly. I'm not sure why but I think its a linking issue. I've tried it in two different web browsers and both show the same outcome. 2022/03/24 6:52 Howdy just wanted to give you a brief heads up and

Howdy just wanted to give you a brief heads up and let you know a few of the pictures aren't loading correctly.

I'm not sure why but I think its a linking issue.

I've tried it in two different web browsers and
both show the same outcome.

# Howdy just wanted to give you a brief heads up and let you know a few of the pictures aren't loading correctly. I'm not sure why but I think its a linking issue. I've tried it in two different web browsers and both show the same outcome. 2022/03/24 6:53 Howdy just wanted to give you a brief heads up and

Howdy just wanted to give you a brief heads up and let you know a few of the pictures aren't loading correctly.

I'm not sure why but I think its a linking issue.

I've tried it in two different web browsers and
both show the same outcome.

# Great post. I used to be checking constantly this blog and I am impressed! Extremely useful information specifically the final phase :) I maintain such info much. I used to be seeking this particular information for a long time. Thanks and good luck. 2022/03/25 8:38 Great post. I used to be checking constantly this

Great post. I used to be checking constantly this blog and I am impressed!
Extremely useful information specifically the final phase :) I maintain such info much.
I used to be seeking this particular information for a long
time. Thanks and good luck.

# Great post. I used to be checking constantly this blog and I am impressed! Extremely useful information specifically the final phase :) I maintain such info much. I used to be seeking this particular information for a long time. Thanks and good luck. 2022/03/25 8:39 Great post. I used to be checking constantly this

Great post. I used to be checking constantly this blog and I am impressed!
Extremely useful information specifically the final phase :) I maintain such info much.
I used to be seeking this particular information for a long
time. Thanks and good luck.

# Great post. I used to be checking constantly this blog and I am impressed! Extremely useful information specifically the final phase :) I maintain such info much. I used to be seeking this particular information for a long time. Thanks and good luck. 2022/03/25 8:40 Great post. I used to be checking constantly this

Great post. I used to be checking constantly this blog and I am impressed!
Extremely useful information specifically the final phase :) I maintain such info much.
I used to be seeking this particular information for a long
time. Thanks and good luck.

# Great post. I used to be checking constantly this blog and I am impressed! Extremely useful information specifically the final phase :) I maintain such info much. I used to be seeking this particular information for a long time. Thanks and good luck. 2022/03/25 8:41 Great post. I used to be checking constantly this

Great post. I used to be checking constantly this blog and I am impressed!
Extremely useful information specifically the final phase :) I maintain such info much.
I used to be seeking this particular information for a long
time. Thanks and good luck.

# These are in fact wonderful ideas in on the topic of blogging. You have touched some pleasant factors here. Any way keep up wrinting. 2022/06/05 5:23 These are in fact wonderful ideas in on the topic

These are in fact wonderful ideas in on the topic of blogging.

You have touched some pleasant factors here. Any way
keep up wrinting.

# I will right away clutch your rss feed as I can not in finding your e-mail subscription link or e-newsletter service. Do you have any? Please permit me recognize so that I may subscribe. Thanks. 2022/06/10 21:28 I will right away clutch your rss feed as I can no

I will right away clutch your rss feed as I can not in finding your e-mail subscription link
or e-newsletter service. Do you have any? Please permit me
recognize so that I may subscribe. Thanks.

# Cool blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple tweeks would really make my blog jump out. Please let me know where you got your design. Cheers 2022/06/11 13:11 Cool blog! Is your theme custom made or did you do

Cool blog! Is your theme custom made or did you download it from somewhere?
A design like yours with a few simple tweeks would really
make my blog jump out. Please let me know where you got your
design. Cheers

# Valuable information. Fortunate me I found your web site by accident, and I'm surprised why this coincidence didn't happened earlier! I bookmarked it. 2022/07/07 20:35 Valuable information. Fortunate me I found your we

Valuable information. Fortunate me I found your web site
by accident, and I'm surprised why this coincidence didn't
happened earlier! I bookmarked it.

# I do not even know the way I finished up here, but I thought this put up used to be great. I do not recognise who you might be however certainly you're going to a famous blogger in case you are not already. Cheers! 2022/07/26 21:24 I do not even know the way I finished up here, but

I do not even know the way I finished up here, but I thought this put up used to be great.
I do not recognise who you might be however
certainly you're going to a famous blogger in case
you are not already. Cheers!

# Hello, Neat post. There's a problem together with your web site in internet explorer, may test this? IE nonetheless is the market leader and a large element of people will miss your magnificent writing because of this problem. 2022/08/02 2:36 Hello, Neat post. There's a problem together with

Hello, Neat post. There's a problem together with your web
site in internet explorer, may test this?
IE nonetheless is the market leader and a
large element of people will miss your magnificent writing because
of this problem.

# Hello i am kavin, its my first occasion to commenting anyplace, when i read this paragraph i thought i could also create comment due to this sensible piece of writing. 2022/08/08 1:48 Hello i am kavin, its my first occasion to comment

Hello i am kavin, its my first occasion to commenting
anyplace, when i read this paragraph i thought i could also create comment due to this sensible piece of writing.

# Hi everyone, it's my first visit at this website, and piece of writing is actually fruitful in favor of me, keep up posting these posts. 2022/08/11 5:58 Hi everyone, it's my first visit at this website,

Hi everyone, it's my first visit at this website, and piece of
writing is actually fruitful in favor of me, keep up
posting these posts.

タイトル
名前
Url
コメント