{"id":2344,"date":"2016-02-14T08:13:25","date_gmt":"2016-02-14T16:13:25","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=2344"},"modified":"2016-03-10T17:24:36","modified_gmt":"2016-03-11T01:24:36","slug":"perl-gotchas-arrays","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=2344","title":{"rendered":"Perl gotcha\u2019s\u2013arrays"},"content":{"rendered":"<p>Working with some existing code to pull data from a MySQL database I ran the following code:<br \/>\n<pre><code class=\"preserve-code-formatting\">\n$SQL&nbsp;&nbsp;= &quot;SELECT id FROM `sensors` &quot;;\n$SQL .= &quot;WHERE `storage_table` LIKE &#039;$source_temperature_table&#039; &quot;;\n\n$r = $db-&gt;query($SQL);\nmy $src_sensor_id = $r-&gt;getNextRow();\nprint &quot;The id is $src_sensor_id \\n&quot;;<\/code><\/pre><\/p>\n<p>No matter which table I ran this on, I always got 1 as the id. So I tried looking at the value of $r using print $r; and the correct ID was showing up. The reason is that perl is different from other languages that I use when it comes to arrays. When I thought I assigned the only value in $r to $src_sensor_id, what I was really doing was assigning the count of the number of rows in $r to $src_sensor_id. When I printed $r, the print statement printed all of the values in the array $r because that\u2019s how print statements handle arrays.<\/p>\n<p>The correct way to assign the array values in $r to variables is to put parentheses around the list of variables in the array. e.g.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nmy ($src_sensor_id) = $r-&gt;getNextRow();<\/code><\/pre><\/p>\n<p>If you have multiple items in the array, separate the variables with commas. e.g.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nmy ($src_sensor_id, $location, $name) = $r-&gt;getNextRow();<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Working with some existing code to pull data from a MySQL database I ran the following code: $SQL&nbsp;&nbsp;= &quot;SELECT id FROM `sensors` &quot;; $SQL .= &quot;WHERE `storage_table` LIKE &#039;$source_temperature_table&#039; &quot;; $r = $db-&gt;query($SQL); my $src_sensor_id = $r-&gt;getNextRow(); print &quot;The id is $src_sensor_id \\n&quot;; No matter which table I ran this on, I always got 1 &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=2344\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Perl gotcha\u2019s\u2013arrays<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[],"class_list":["post-2344","post","type-post","status-publish","format-standard","hentry","category-perl"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2344"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2344\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}