ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hi
I just learn perl script
May i know how to simplify the code below especially in the red color part?
i saw some examples in internet, they use "next" command
#!/usr/bin/perl
use warnings ;
$infile = "foo" ;
open(INFILE, $infile) || die "Problem opening file: $!" ;
@lines = <INFILE> ;
close INFILE ;
foreach(@lines) {
print if /bar/ ;
}
You're using @ARGV, et al., but I'm sure you can piece main points together. Also, just want to confirm you are aware that you are sucking the entire file into memory before processing it.
(NB: I am a perl novice. There may be cleverer ways still to tackle this problem. But what I posted should be acceptable.)
I agree. I thought maybe he was doing it for performance reasons (RAM access vs. disk I/O), but in looking at OP's background and the context here, that is unlikely.
markush's modification is probably best in this case.
In general one can say that with Perl most often the shortest code is the most efficient. Shortcuts are not only "syntactical sugar" but are made for the efficiency.
#!/apps/perl/bin/perl
use strict;
use warnings;
system(clear);
my $infile = "ttt.txt";
open( INFILE, $infile) || die("Problem opening file: $ !");
while(<INFILE>) {
@lines = <INFILE>;
print @lines;
}
close(INFILE);
You should read the manuals in the link which Sergei Steshenko posted!
In the above code you see a typical situation in Perl. If it is clear what you mean, you don't have to say it (like in human languages). Within a loop the variable $_ stores the iterated variable, you can write "print $_;" but even this is too much because in this context it is clear that $_ is meant, so you don't have to write $_
Also note that it is important to declare a variable with the "my" keyword. It tells perl that this variable is local to this (file) package. And "use strict;" will give you hints about this, at least while learning Perl you should use it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.