{"id":2419,"date":"2017-12-08T10:02:42","date_gmt":"2017-12-08T10:02:42","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=2419"},"modified":"2020-01-09T09:50:07","modified_gmt":"2020-01-09T09:50:07","slug":"foreach-vs-for-vs-while-in-perl","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/foreach-vs-for-vs-while-in-perl\/","title":{"rendered":"Foreach Vs for Vs while in perl"},"content":{"rendered":"<p><strong>rajeshkumar created the topic: foreach Vs for Vs while in perl<\/strong><\/p>\n<p>Definition 1:<br \/>\nIn the while loop, Perl reads a line of input, puts it into a variable, and runs the body of the loop. Then, it goes back to find another line of input.<\/p>\n<p>But in the foreach loop, the line-input operator is being used in a list context (since foreach needs a list to iterate through). So it has to read all of the input before the loop can start running.<\/p>\n<p>That difference will become apparent when the input is coming from your 400 MB web server logfile! It&#8217;s generally best to use code like the while loop&#8217;s shortcut, which will process input a line at a time, whenever possible. Source-The Llama book<\/p>\n<p>Definition 2:<br \/>\nWhat Alex Reynolds says in stackoverflow.com<br \/>\nFor most purposes, you probably won&#8217;t notice a difference.<br \/>\nHowever, foreach reads each line into a list (not an array) before going through it line by line, whereas while reads one line at a time.<br \/>\nAs foreach will use more memory and require processing time upfront, it is generally recommended to use while to iterate through lines of a file.<\/p>\n<p>In addition to the previous responses, another benefit of using while is that you can use the $. variable. This is the current line number of the last filehandle accessed<\/p>\n<p><code>while ( my $line = <FILE> ) {<br \/>\n    if ( $line =~ \/some_target\/ ) {<br \/>\n        print \"Found some_target at line $.\\n\";<br \/>\n    }<br \/>\n}<\/code><\/p>\n<p>Regards,<br \/>\nRajesh Kumar<br \/>\nTwitt me @<a href=\"http:\/\/twitter.com\/RajeshKumarIn\" target=\"_blank\" rel=\"noopener\"> twitter.com\/RajeshKumarIn<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>rajeshkumar created the topic: foreach Vs for Vs while in perl Definition 1: In the while loop, Perl reads a line of input, puts it into a variable, and runs the body of the loop. Then, it goes back to find another line of input. But in the foreach loop, the line-input operator is being&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[14],"tags":[175],"class_list":["post-2419","post","type-post","status-publish","format-standard","hentry","category-perl","tag-perl"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=2419"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2419\/revisions"}],"predecessor-version":[{"id":2420,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2419\/revisions\/2420"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=2419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=2419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=2419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}