{"id":68372,"date":"2022-11-04T09:01:51","date_gmt":"2022-11-04T09:01:51","guid":{"rendered":"https:\/\/www.cryptocabaret.com\/?p=68372"},"modified":"2022-11-04T09:01:51","modified_gmt":"2022-11-04T09:01:51","slug":"how-to-iterate-over-tables-in-lua","status":"publish","type":"post","link":"https:\/\/www.cryptocabaret.com\/?p=68372","title":{"rendered":"How to iterate over tables in Lua"},"content":{"rendered":"<p><span class=\"field field--name-title field--type-string field--label-hidden\">How to iterate over tables in Lua<\/span><br \/>\n<span class=\"field field--name-uid field--type-entity-reference field--label-hidden\"><a title=\"View user profile.\" href=\"https:\/\/opensource.com\/users\/seth\" class=\"username\">Seth Kenlon<\/a><\/span><br \/>\n<span class=\"field field--name-created field--type-created field--label-hidden\">Fri, 11\/04\/2022 &#8211; 03:00<\/span><\/p>\n<div class=\"clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item\">\n<p>In the <a href=\"https:\/\/opensource.com\/article\/22\/11\/lua-worth-learning\" target=\"_blank\" rel=\"noopener\">Lua<\/a> programming language, an array is called a table. A table is used in Lua to store data. If you&#8217;re storing a lot of data in a structured way, it&#8217;s useful to know your options for retrieving that data when you need it.<\/p>\n<\/p>\n<div class=\"embedded-resource-list callout-float-right\">\n<div class=\"field field--name-title field--type-string field--label-hidden field__item\">Programming and development<\/div>\n<div class=\"field field--name-links field--type-link field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/developers.redhat.com\/?intcmp=7016000000127cYAAQ\">Red Hat Developers Blog<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/cheat-sheets?intcmp=7016000000127cYAAQ\">Programming cheat sheets<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/en\/services\/training\/learning-subscription?intcmp=7016000000127cYAAQ\">Try for free: Red Hat Learning Subscription<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/bash-programming-guide?intcmp=7016000000127cYAAQ\">eBook: An introduction to programming with Bash<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/developers.redhat.com\/cheat-sheets\/bash-shell-cheat-sheet?intcmp=7016000000127cYAAQ\">Bash shell scripting cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/developers.redhat.com\/e-books\/modernizing-enterprise-java?intcmp=7016000000127cYAAQ\">eBook: Modernizing Enterprise Java<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/building-applications?intcmp=7016000000127cYAAQ\">An open source developer&#8217;s guide to building applications<\/a><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<h2>Creating a table in Lua<\/h2>\n<p>To create a table in Lua, you instantiate the table with an arbitrary name:<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"c geshifilter-c\">mytable <span class=\"sy0\">=<\/span> <span class=\"br0\">{<\/span><span class=\"br0\">}<\/span><\/code><\/span><\/pre>\n<p>There are different ways you can structure your data in a table. You could fill it with values, essentially creating a list (called a <em>list<\/em> in some languages):<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"c geshifilter-c\">mytable <span class=\"sy0\">=<\/span> <span class=\"br0\">{<\/span> <span class=\"st0\">'zombie'<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">'apocalypse'<\/span> <span class=\"br0\">}<\/span><\/code><\/span><\/pre>\n<p>Or you could create an associated array (called a <em>map<\/em> or <em>dictionary<\/em> in some languages). You can add arbitrary keys to the table using dot notation. You can also add a value to that key the same way you add a value to a variable:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"c geshifilter-c\">myarray <span class=\"sy0\">=<\/span> <span class=\"br0\">{<\/span><span class=\"br0\">}<\/span><br>\nmyarray.<span class=\"me1\">baz<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">'happy'<\/span><br>\nmyarray.<span class=\"me1\">qux<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">'halloween'<\/span><\/div><\/div><\/pre>\n<p>You can add verification with the <code>assert()<\/code> function:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"c geshifilter-c\"><a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/assert.html\"><span class=\"kw3\">assert<\/span><\/a><span class=\"br0\">(<\/span>myarray.<span class=\"me1\">baz<\/span> <span class=\"sy0\">==<\/span> <span class=\"st0\">'happy'<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">'unexpected value in myarray.baz'<\/span><span class=\"br0\">)<\/span><br><a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/assert.html\"><span class=\"kw3\">assert<\/span><\/a><span class=\"br0\">(<\/span>myarray.<span class=\"me1\">qux<\/span> <span class=\"sy0\">==<\/span> <span class=\"st0\">'halloween'<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">'unexpected value in myarray.qux'<\/span><span class=\"br0\">)<\/span><\/div><\/div><\/pre>\n<p>You now have two tables: a list-style <code>mytable<\/code> and an associative array-style <code>myarray<\/code>.<\/p>\n<h2>Iterating over a table with pairs<\/h2>\n<p>Lua&#8217;s <code>pairs()<\/code> function extracts key and value pairs from a table.<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"c geshifilter-c\">print<span class=\"br0\">(<\/span><span class=\"st0\">'pairs of myarray:'<\/span><span class=\"br0\">)<\/span><br><br><span class=\"kw1\">for<\/span> k<span class=\"sy0\">,<\/span>v in pairs<span class=\"br0\">(<\/span>myarray<span class=\"br0\">)<\/span> <span class=\"kw1\">do<\/span><br>\n\u00a0 print<span class=\"br0\">(<\/span>k<span class=\"sy0\">,<\/span>v<span class=\"br0\">)<\/span><br>\nend<\/div><\/div><\/pre>\n<p>Here&#8217;s the output:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"c geshifilter-c\">pairs of myarray<span class=\"sy0\">:<\/span><br>\nbaz \u00a0 \u00a0 happy<br>\nqux \u00a0 \u00a0 halloween<\/div><\/div><\/pre>\n<p>If there are no keys in a table, Lua uses an index. For instance, the <code>mytable<\/code> table contains the values <code>zombie<\/code> and <code>apocalypse<\/code>. It contains no keys, but Lua can improvise:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"c geshifilter-c\">print<span class=\"br0\">(<\/span><span class=\"st0\">'pairs of mytable:'<\/span><span class=\"br0\">)<\/span><br><br><span class=\"kw1\">for<\/span> k<span class=\"sy0\">,<\/span>v in pairs<span class=\"br0\">(<\/span>mytable<span class=\"br0\">)<\/span> <span class=\"kw1\">do<\/span><br>\n\u00a0 print<span class=\"br0\">(<\/span>k<span class=\"sy0\">,<\/span>v<span class=\"br0\">)<\/span><br>\nend<\/div><\/div><\/pre>\n<p>Here&#8217;s the output:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"c geshifilter-c\"><span class=\"nu0\">1<\/span> \u00a0 zombie<br><span class=\"nu0\">2<\/span> \u00a0 apocalypse<\/div><\/div><\/pre>\n<h2>Iterating over a table with ipairs<\/h2>\n<p>To account for the fact that tables without keys are common, Lua also provides the <code>ipairs<\/code> function. This function extracts the index and the value:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"c geshifilter-c\">print<span class=\"br0\">(<\/span><span class=\"st0\">'ipairs of mytable:'<\/span><span class=\"br0\">)<\/span><br><br><span class=\"kw1\">for<\/span> i<span class=\"sy0\">,<\/span>v in ipairs<span class=\"br0\">(<\/span>mytable<span class=\"br0\">)<\/span> <span class=\"kw1\">do<\/span><br>\n\u00a0 print<span class=\"br0\">(<\/span>i<span class=\"sy0\">,<\/span>v<span class=\"br0\">)<\/span><br>\nend<\/div><\/div><\/pre>\n<p>The output is, in this case, the same as the output of <code>pairs<\/code>:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"c geshifilter-c\"><span class=\"nu0\">1<\/span> \u00a0 zombie<br><span class=\"nu0\">2<\/span> \u00a0 apocalypse<\/div><\/div><\/pre>\n<p>However, watch what happens when you add a key and value pair to <code>mytable<\/code>:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"c geshifilter-c\">mytable.<span class=\"me1\">surprise<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">'this value has a key'<\/span><br><br>\nprint<span class=\"br0\">(<\/span><span class=\"st0\">'ipairs of mytable:'<\/span><span class=\"br0\">)<\/span><br><br><span class=\"kw1\">for<\/span> i<span class=\"sy0\">,<\/span>v in ipairs<span class=\"br0\">(<\/span>mytable<span class=\"br0\">)<\/span> <span class=\"kw1\">do<\/span><br>\n\u00a0 print<span class=\"br0\">(<\/span>i<span class=\"sy0\">,<\/span>v<span class=\"br0\">)<\/span><br>\nend<\/div><\/div><\/pre>\n<p>Lua ignores the key and value because <code>ipairs<\/code> retrieves <em>only<\/em> indexed entries:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"c geshifilter-c\"><span class=\"nu0\">1<\/span> \u00a0 zombie<br><span class=\"nu0\">2<\/span> \u00a0 apocalypse<\/div><\/div><\/pre>\n<p>The key and value pair, however, have been stored in the table:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"c geshifilter-c\">print<span class=\"br0\">(<\/span><span class=\"st0\">'pairs of mytable:'<\/span><span class=\"br0\">)<\/span><br><br><span class=\"kw1\">for<\/span> k<span class=\"sy0\">,<\/span>v in ipairs<span class=\"br0\">(<\/span>mytable<span class=\"br0\">)<\/span> <span class=\"kw1\">do<\/span><br>\n\u00a0 print<span class=\"br0\">(<\/span>k<span class=\"sy0\">,<\/span>v<span class=\"br0\">)<\/span><br>\nend<\/div><\/div><\/pre>\n<p>The output:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"c geshifilter-c\"><span class=\"nu0\">1<\/span> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0zombie<br><span class=\"nu0\">2<\/span> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0apocalypse<br>\nsurprise \u00a0 this value has a key<\/div><\/div><\/pre>\n<h2>Retrieving arbitrary values<\/h2>\n<p>You don&#8217;t have to iterate over a table to get data out of it. You can call arbitrary data by either index or key:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"c geshifilter-c\">print<span class=\"br0\">(<\/span><span class=\"st0\">'call by index:'<\/span><span class=\"br0\">)<\/span><br>\nprint<span class=\"br0\">(<\/span>mytable<span class=\"br0\">[<\/span><span class=\"nu0\">2<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><br>\nprint<span class=\"br0\">(<\/span>mytable<span class=\"br0\">[<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><br>\nprint<span class=\"br0\">(<\/span>myarray<span class=\"br0\">[<\/span><span class=\"nu0\">2<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><br>\nprint<span class=\"br0\">(<\/span>myarray<span class=\"br0\">[<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><br><br>\nprint<span class=\"br0\">(<\/span><span class=\"st0\">'call by key:'<\/span><span class=\"br0\">)<\/span><br>\nprint<span class=\"br0\">(<\/span>myarray<span class=\"br0\">[<\/span><span class=\"st0\">'qux'<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><br>\nprint<span class=\"br0\">(<\/span>myarray<span class=\"br0\">[<\/span><span class=\"st0\">'baz'<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><br>\nprint<span class=\"br0\">(<\/span>mytable<span class=\"br0\">[<\/span><span class=\"st0\">'surprise'<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><\/div><\/div><\/pre>\n<p>The output:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"c geshifilter-c\">call by index<span class=\"sy0\">:<\/span><br>\napocalypse<br>\nzombie<br>\nnil<br>\nnil<br><br>\ncall by key<span class=\"sy0\">:<\/span><br>\nhalloween<br>\nhappy<br>\nthis value has a key<\/div><\/div><\/pre>\n<h2>Data structures<\/h2>\n<p>Sometimes using a Lua table makes a lot more sense than trying to keep track of dozens of individual variables. Once you understand how to structure and retrieve data in a language, you&#8217;re empowered to generate complex data in an organized and safe way.<\/p>\n<\/div>\n<div class=\"clearfix text-formatted field field--name-field-article-subhead field--type-text-long field--label-hidden field__item\">\n<p>Create structure that makes it easier to find stored data.<\/p>\n<\/div>\n<div class=\"field field--name-field-lead-image field--type-entity-reference field--label-hidden field__item\">\n<article class=\"media media--type-image media--view-mode-caption\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/11\/programming_keyboard_coding.png\" width=\"520\" height=\"292\" alt=\"Programming keyboard.\" title=\"Programming keyboard.\"><\/div>\n<div class=\"field field--name-field-caption field--type-text-long field--label-hidden caption field__item\"><span class=\"caption__byline\">Image by: <\/span><\/p>\n<p>Opensource.com<\/p>\n<\/div>\n<\/article>\n<\/div>\n<div class=\"field field--name-field-tags field--type-entity-reference field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/programming\" hreflang=\"en\">Programming<\/a><\/div>\n<\/p><\/div>\n<div class=\"hidden field field--name-field-listicle-title field--type-string field--label-hidden field__item\">What to read next<\/div>\n<div class=\"field field--name-field-default-license field--type-list-string field--label-hidden field__item\"><a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\"><br \/>\n        <img decoding=\"async\" alt=\"Creative Commons License\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/11\/cc-by-sa-4-5.png\" title=\"This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.\"><\/a>This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.<\/div>\n<section class=\"field field--name-field-comments field--type-comment field--label-hidden comment-wrapper\">\n<div class=\"comments__count\">\n<div class=\"login\"><a href=\"https:\/\/opensource.com\/user\/register?absolute=1\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?destination=\/feed&amp;absolute=1\">Login<\/a> to post a comment.<\/div>\n<\/p><\/div>\n<\/section>\n<p class=\"wpematico_credit\"><small>Powered by <a href=\"http:\/\/www.wpematico.com\" target=\"_blank\" rel=\"noopener\">WPeMatico<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to iterate over tables in Lua Seth Kenlon Fri, 11\/04\/2022 &#8211; 03:00 In the Lua programming language, an array is called a table. A table is used in Lua to store data. If you&#8217;re storing a lot of data in a structured way, it&#8217;s useful to know your options for retrieving that data when [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":68373,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[307],"tags":[],"class_list":["post-68372","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-open-source"],"_links":{"self":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/68372","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=68372"}],"version-history":[{"count":0,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/68372\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/media\/68373"}],"wp:attachment":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=68372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=68372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=68372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}