Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

Foreach Vs for Vs while in perl

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 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.

That difference will become apparent when the input is coming from your 400 MB web server logfile! It’s generally best to use code like the while loop’s shortcut, which will process input a line at a time, whenever possible. Source-The Llama book

Definition 2:
What Alex Reynolds says in stackoverflow.com
For most purposes, you probably won’t notice a difference.
However, 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.
As foreach will use more memory and require processing time upfront, it is generally recommended to use while to iterate through lines of a file.

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

while ( my $line = ) {
if ( $line =~ /some_target/ ) {
print "Found some_target at line $.\n";
}
}

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals

Similar Posts

  • Use of STDIN in perl

    scmuser created the topic: Use of STDIN in perl What are the various ways we can use STDIN in perl. Could you please provide some examples? Rajesh KumarI’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories at Holiday Landmark, stock…

  • Perl -MCPAN -e “install DBD::mysql”

    rajeshkumar created the topic: perl -MCPAN -e “install DBD::mysql” I am getting following issues while running following command in Windows Machine… perl -MCPAN -e “install DBD::mysql” Error Regards, Rajesh Kumar Twitt me @ twitter.com/RajeshKumarIn Rajesh KumarI’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at…

  • Good Books on Perl

    rajeshkumar created the topic: Good Books on Perl 1. Effective Perl Programming: Ways to Write Better, More Idiomatic Perl (2nd Edition) (Effective Software Development Series) Regards, Rajesh Kumar Twitt me @ twitter.com/RajeshKumarIn Rajesh KumarI’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School,…

  • How to Check File Attributes in Perl ? Perl File Attributes explained

    Checking File Attributes in Perl I have been using perl for quite some time now. I have also been using the file handling logic in my scripts. However, what I did not know till now is that you can quickly check for certain file properties in perl. Here is an example: #!/usr/bin/perl my (@description,$size); if (-e…

  • $_ and @_ in perl

    scmuser created the topic: $_ and @_ in perl Hi, Can you please talk about $_ and @_ in perl? Rajesh KumarI’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health…

  • Send an HTML email using perl and sendmail

    rajeshkumar created the topic: Send an HTML email using perl and sendmail Send an HTML email using perl and sendmail open(MAIL,”|/usr/sbin/sendmail -t”); ## Mail Header print MAIL “To: rajeshk\@xxx.com\n”; print MAIL “From: rajeshk\@xxxx.com\n”; print MAIL “Subject: TEST\n”; ## Mail Body print MAIL “Content-Type: text/html; charset=ISO-8859-1\n” . “@emailBody”; close(MAIL) Regards, Rajesh Kumar Twitt me @ twitter.com/RajeshKumarIn…

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments