{"id":12954,"date":"2015-11-04T20:30:45","date_gmt":"2015-11-04T20:30:45","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=12954"},"modified":"2015-11-04T20:30:45","modified_gmt":"2015-11-04T20:30:45","slug":"perl-array-operations-push-and-pop","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/perl-array-operations-push-and-pop\/","title":{"rendered":"Perl:  array operations:  push and pop"},"content":{"rendered":"<h1>According to Perl, an idea can push into your head, then pop out.<\/h1>\n<p>For previous information about arrays, just search this site.  You may want to start <a href=\"?p=5096\">here<\/a>.  For information about getting Perl up and running on your computer, try Perl in the search box at the right:)<\/p>\n<p>With Perl, you can add an element to the end of an array using <span style=\"font-family:monospace\">push<\/span>.  Let&#8217;s imagine you want to add <span style=\"font-family:monospace\">&#8220;Hans&#8221;<\/span> to the array <span style=\"font-family:monospace\">@guestlist<\/span>.  You might do so as follows:<\/p>\n<p><span style=\"font-family:monospace\">push(@guestlist, &#8220;Hans&#8221;);<\/span><\/p>\n<p>Likewise, you can remove the last element from an array using <span style=\"font-family:monospace\">pop<\/span>.  Perhaps, now,  you want to remove Hans from <span style=\"font-family:monospace\">@guestlist<\/span> and store him in the variable <span style=\"font-family:monospace\">$scratched<\/span>.  You might do so this way:<\/p>\n<p><span style=\"font-family:monospace\">$scratched=pop(@guestlist);<\/span><\/p>\n<p>Via the Perl program below, the user can ride the roller-coaster of forming a guest list for a party.  Note that the guests are added to the array <span style=\"font-family:monospace\">@partyguests<\/span> using <span style=\"font-family:monospace\">push<\/span>, while the last one is later removed using <span style=\"font-family:monospace\">pop<\/span>:<\/p>\n<p><span style=\"font-family:monospace;color:brown\">#!\/usr\/bin\/perl<br \/>\n<br \/>\n@partyguests=qw(Bill Trish Steve Flo);<br \/>\n<br \/>\nprint &#8220;\\n\\nHere&#8217;s our guest list: @partyguests.\\n\\n&#8221;;<br \/>\n<br \/>\nprint &#8220;Let&#8217;s invite Tom, Sheila, and Suzy.\\n\\n&#8221;;<br \/>\n<br \/>\npush (@partyguests,&#8221;Tom&#8221;);<br \/>\n<br \/>\npush(@partyguests,&#8221;Sheila&#8221;);<br \/>\n<br \/>\npush(@partyguests,&#8221;Suzy&#8221;);<br \/>\n<br \/>\nprint &#8220;Here&#8217;s our guest list with the additions: @partyguests.\\n\\n&#8221;;<br \/>\n<br \/>\nprint &#8220;Oh, no:  there&#8217;s only room for six.  We&#8217;ll scratch Suzy:(\\n\\n&#8221;;<br \/>\n<br \/>\n$scratched=pop(@partyguests);<br \/>\n<br \/>\nprint &#8220;We&#8217;ll make it up to you, $scratched.\\n\\n&#8221;;<br \/>\n<br \/>\nprint &#8220;This is our updated guest list: @partyguests.\\n\\n&#8221;;<br \/>\n<\/span><\/p>\n<p>Don&#8217;t worry, Suzy;  from what I heard, you didn&#8217;t miss much:)<\/p>\n<p>I&#8217;ll be talking more about Perl array operations.<\/p>\n<p>Source:<\/p>\n<p>McGrath, Mike.  <u>Perl in easy steps<\/u>.  Southam:  Computer Step, 2004.<\/p>\n<p>Jack of <a href=\"https:\/\/www.oracletutoring.ca\">Oracle Tutoring by Jack and Diane,<\/a> Campbell River, BC.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>According to Perl, an idea can push into your head, then pop out. For previous information &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/perl-array-operations-push-and-pop\/\"> <span class=\"screen-reader-text\">Perl:  array operations:  push and pop<\/span> Read more \u2192<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[1170,173,1172,1171],"class_list":["post-12954","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-perl-array-operations","tag-perl-programming","tag-pop","tag-push"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/12954","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/comments?post=12954"}],"version-history":[{"count":36,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/12954\/revisions"}],"predecessor-version":[{"id":12990,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/12954\/revisions\/12990"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=12954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=12954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=12954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}