LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-02-2011, 05:29 AM   #1
casualzone
Member
 
Registered: Jan 2010
Posts: 189

Rep: Reputation: 15
perl script to read line by line


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


#!/apps/perl/bin/perl
system(clear);
open($INPUT, $ARGV[0]) || die('No input_net file');
@INPUT_FILE=<$INPUT>;
$INPUT_LINE=@INPUT_FILE;
close($INPUT);

$line=0;
while($line < $INPUT_LINE)
{

if(@INPUT_FILE[$line] =~ m/abcd/ )
{
do_something;
}

}
 
Old 08-02-2011, 03:30 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Here's a slightly different approach:
Code:
#!/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.)
 
Old 08-02-2011, 03:43 PM   #3
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Code:
#!/usr/bin/perl

use warnings ;

$infile = "foo" ;

open(INFILE, $infile) || die "Problem opening file: $!" ;
@lines = <INFILE> ;


while(<INFILE>) {

  print if /bar/ ;

}
close INFILE ;
Quote:
(NB: I am a perl novice. There may be cleverer ways still to tackle this problem. But what I posted should be acceptable.)
Well, it is not necessary to store the INFILE in an array.

Markus
 
1 members found this post helpful.
Old 08-02-2011, 03:49 PM   #4
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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.
 
Old 08-02-2011, 03:55 PM   #5
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hello anomie,

the code as I wrote it is always used in such cases in Perl-tutorials and also in the Camel-book http://oreilly.com/catalog/9780596000271

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.

Markus
 
1 members found this post helpful.
Old 08-02-2011, 07:10 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 08-02-2011, 11:34 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by casualzone View Post
hi
I just learn perl script
...
http://perldoc.perl.org/ -> http://perldoc.perl.org/index-overview.html -> http://perldoc.perl.org/perlintro.html
http://perldoc.perl.org/perlopentut.html - this one has examples you need.

First refer to that official http://perldoc.perl.org/ site.
 
Old 08-03-2011, 12:06 AM   #8
casualzone
Member
 
Registered: Jan 2010
Posts: 189

Original Poster
Rep: Reputation: 15
thank you very much
need to spend some time to pick up this skill
 
Old 08-03-2011, 12:23 AM   #9
casualzone
Member
 
Registered: Jan 2010
Posts: 189

Original Poster
Rep: Reputation: 15
the input file is not a fixed file name eg "foo", it is an user input file that can be any file name

For this case, is it true that i got to use $ARGV[0]?
 
Old 08-03-2011, 12:28 AM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by casualzone View Post
the input file is not a fixed file name eg "foo", it is an user input file that can be any file name

For this case, is it true that i got to use $ARGV[0]?
Using @ARGV is a right thing to do, but do not reinvent the wheel:


 
Old 08-03-2011, 12:33 AM   #11
casualzone
Member
 
Registered: Jan 2010
Posts: 189

Original Poster
Rep: Reputation: 15
just test the code:

#!/apps/perl/bin/perl

use warnings;
system(clear);

$infile = "ttt.txt";
open( INFILE, $infile) || die("Problem opening file: $ !");

while(<INFILE>) {
@lines = <INFILE>;
print @lines;
}
close(INFILE);

---------------
ttt.txt file
------------
hello
##############
heheheheheh
hehehehe
hehehe
hehe
he
########

-------
RESULT
-------
##############
heheheheheh
hehehehe
hehehe
hehe
he
########

WHY THE FIRST LINE IS MISSING?
 
Old 08-03-2011, 12:44 AM   #12
casualzone
Member
 
Registered: Jan 2010
Posts: 189

Original Poster
Rep: Reputation: 15
another test case:

#!/apps/perl/bin/perl
system(clear);

open( INFILE, $ARGV[0] ) || die("Can't open file for read.\n");

while (<INFILE>)
{
$line = <INFILE>;
print("-- $line");
}

close(INFILE);

-------
OUTPUT
--------
-- ##############
-- hehehehe
-- hehe
-- ########

WHY THE ODD LINES IS MISSING?
 
Old 08-03-2011, 12:50 AM   #13
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hi,

you've written much too much code

try this
Code:
#!/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.

Markus

Last edited by markush; 08-03-2011 at 12:51 AM.
 
Old 08-03-2011, 01:05 AM   #14
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hi, to your above examples,

in the first case you use the complete INFILE as an array, in this case no loop is needed.
Code:
while(<INFILE>) {
       @lines = <INFILE>;
       print @lines;
}
in the second case you use the complete INFILE as a scalar value.
Code:
$line = <INFILE>
I don't even understand what this code does, but most likely it is not what you wanted to do.

Markus
 
Old 08-03-2011, 01:14 AM   #15
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by markush View Post
...
in the second case you use the complete INFILE as a scalar value.
Code:
$line = <INFILE>
I don't even understand what this code does, but most likely it is not what you wanted to do.

Markus
Not necessarily complete. By default just one line is read.
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
Perl script to replace a line after finding a previous line Mark1986 Programming 1 02-28-2011 05:09 PM
How to do a script which read line by line /passwd using while giantjavi Linux - Newbie 9 05-15-2010 07:30 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration