AR/Table: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 107: | Line 107: | ||
</syntaxhighlight> </section> | </syntaxhighlight> </section> | ||
<p align="center">___________________________________________________________________</p> | |||
| Line 139: | Line 139: | ||
<p align="center">___________________________________________________________________</p> | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
table table.remove (table,function) | table table.remove (table,function) | ||
| Line 170: | Line 170: | ||
</syntaxhighlight> </section> | </syntaxhighlight> </section> | ||
<p align="center">___________________________________________________________________</p> | |||
'''== table.sort == | |||
تستخدم لـ ترتيب الجدول | |||
<syntaxhighlight lang="lua"> | |||
table table.sort(table,function) | |||
</syntaxhighlight> | |||
<section name="Example" class="both" show="true"> | |||
<syntaxhighlight lang="lua"> | |||
t = {5,1,7,2,9,8,3} -- صنع جدول داخله ارقام عشوائية | |||
table.sort(t, | |||
function (a,b) | |||
return ( tonumber(a) or 0 ) < ( tonumber(b) or 0 ) -- يرتبهم حسب الاصغر | |||
end | |||
) | |||
-- النتيجة | |||
-- t = {1,2,3,5,7,8,9} | |||
</syntaxhighlight> </section> | |||
<div style="background: #FFCFCF; padding: 5px; font-weight:bold; border: 1px dotted #AAAAAA;padding:10px;margin:10px;"><center> | <div style="background: #FFCFCF; padding: 5px; font-weight:bold; border: 1px dotted #AAAAAA;padding:10px;margin:10px;"><center> | ||
| Line 188: | Line 202: | ||
* [http://www.lua.org/manual/5.1/manual.html#pdf-table.concat table.concat] | * [http://www.lua.org/manual/5.1/manual.html#pdf-table.concat table.concat] | ||
* [http://www.lua.org/manual/5.1/manual.html#pdf-table.maxn table.maxn] | * [http://www.lua.org/manual/5.1/manual.html#pdf-table.maxn table.maxn] | ||
* [http://www.lua.org/manual/5.1/manual.html#pdf-unpack unpack] | * [http://www.lua.org/manual/5.1/manual.html#pdf-unpack unpack] | ||
Revision as of 09:43, 3 January 2014
بسم الله الرحمن الرحيم
___________________________________________________________________
== table.foreachi ==
تستخدم ل إظهار الجدول بصف واحد
table table.foreachi(table,function)
Click to collapse [-]
Example
t = {1,1,2,3,5,8,13}
test1 = table.foreachi(t,print)
1 1
2 1
3 2
4 3
5 5
6 8
7 13
Click to collapse [-]
Example
t = { 1,2,"three"; pi=3.14159, banana="yellow" }
test1 = table.foreachi(t,print)
1 1
2 2
3 three
___________________________________________________________________
table.foreach
تستخدم ل تكرار المفاتيح في الجدول
table table.foreach(table,function)
Click to collapse [-]
Example
k = { apple="green", orange="orange", banana="yellow" }
test1 = table.foreach(k,print)
apple green
orange orange
banana yellow
Click to collapse [-]
Example
t = { 1,2,"three"; pi=3.14159, banana="yellow" }
test1 = table.foreach(t,print)
1 1
2 2
3 three
pi 3.14159
banana yellow
___________________________________________________________________
table table.insert (table,function)
Click to collapse [-]
Example-- مثال على جلب أسماء اللاعبين عن طريق الجدول
t = { }; -- إنشاء جدول
for i, p in ipairs ( getElementsByType ( "player" ) ) do -- لوب للاعبين
table.insert ( t, p ); -- نحطهم ف الجدول
end -- إغلاق loop
------------------------
addCommandHandler ( "gPlayer", function ( ) -- إنشاء أمر
for i, p in ipairs ( t ) do -- نجيب الا ف الجدول
if p ~= 0 then -- نتأكد أن الجدول لا يساوي صفر أو مو فاضي
outputChatBox ( getPlayerName ( p ) ); -- نطلع الأسماء في الشات
end -- إغلاق if
end -- إغلاق loop
end -- إغلاق function
); -- إغلاق القوس
___________________________________________________________________
table table.remove (table,function)
Click to collapse [-]
Example-- مثال على حذف إسم اللاعب من الجدول
t = { }; -- إنشاء جدول
for i, p in ipairs ( getElementsByType ( "player" ) ) do -- لوب للاعبين
table.insert ( t, p ); -- نحط اللاعبين ف الجدول
end -- إغلاق loop
------------------------
addCommandHandler ( "removeMe", function ( player ) -- إنشاء أمر
for i, p in ipairs ( t ) do -- نجيب الا ف الجدول
if p ~= 0 then -- نتأكد أن الجدول لا يساوي صفر أو مو فاضي
if player == p then -- إذا كان إسم اللاعب الي كتب الأمر يساوي اللاعب اف الجدول
table.remove ( t, i ); -- نحذفه من الجدول بواسطة رقمه
end -- إغلاق if
end -- إغلاق if
end -- إغلاق loop
end -- إغلاق function
); --
___________________________________________________________________
== table.sort ==
تستخدم لـ ترتيب الجدول
table table.sort(table,function)
Click to collapse [-]
Examplet = {5,1,7,2,9,8,3} -- صنع جدول داخله ارقام عشوائية
table.sort(t,
function (a,b)
return ( tonumber(a) or 0 ) < ( tonumber(b) or 0 ) -- يرتبهم حسب الاصغر
end
)
-- النتيجة
-- t = {1,2,3,5,7,8,9}
؟ table.foreachi و table.foreach الفرق بين
الاولى تستخدم لفهرسة الجدول بينما الاخرى هي لتكرار مفاتيح الجداول ,, ملاحظة : الثانيه غير مضمونه بما يتعلق الترتيب التي يتم تخزين مفاتيح في جدول