perl commands

G

Gabriella

Could somebody give me the meaning of each line ?

while (<IN>) {
my $line = $_;
chomp($line);
next if ($line =~ /^ *$/m);
next if ($line =~ /^#.*$/m);
next if ($line !~ /^[0-9].*$/m);
#$line =~ s/#.*$//m;
$line =~ s/ +$//m;
$line =~ s/^ +//m;
Thanks
 
K

Kim Schulz

On 2 Mar 2005 13:35:34 -0800
Could somebody give me the meaning of each line ?

while (<IN>) {

While reading input (eg file on commandline)

my $line = $_;
set $line to be the next(first) line in the file
chomp($line);
remove newline from the end of the line.
next if ($line =~ /^ *$/m);
ignore line and goto next line if line only contains spaces or is empty
next if ($line =~ /^#.*$/m);
ignore line if line starts with # (comment probably)
next if ($line !~ /^[0-9].*$/m);
ignore line if it does not start with a number
#$line =~ s/#.*$//m; a comment (notice the #)
$line =~ s/ +$//m;
remove all leftover spaces at the ends of line
$line =~ s/^ +//m;
remove all spaces at the beginning of the line
 
J

Jim Keenan

Gabriella said:
Could somebody give me the meaning of each line ?

while (<IN>) {
my $line = $_;
chomp($line);
next if ($line =~ /^ *$/m);
next if ($line =~ /^#.*$/m);
next if ($line !~ /^[0-9].*$/m);
#$line =~ s/#.*$//m;
$line =~ s/ +$//m;
$line =~ s/^ +//m;
Thanks

Better than giving you the answers ... we'll show you how to look up
the answers yourself in the Perl documentation. Assuming you have Perl
installed, you call the 'perldoc' command from the command-prompt,
followed by one or more options or arguments. For example:

perldoc -f chomp # Documentation for function 'chomp'
perldoc perlop # Documentation for Perl operators
# Look in sections on "Binding Operators" and
# "Regexp Quote-Like Operators"
perldoc perldoc # How to use the perldoc command
perldoc perltoc # Table of contents for the Perl documentation

And so forth.
 
L

Lars Eighner

In our last episode,
the lovely and said:
On 2 Mar 2005 13:35:34 -0800
(e-mail address removed) (Gabriella) wrote:
While reading input (eg file on commandline)
my $line = $_;
set $line to be the next(first) line in the file
chomp($line);
remove newline from the end of the line.
next if ($line =~ /^ *$/m);
ignore line and goto next line if line only contains spaces or is empty
next if ($line =~ /^#.*$/m);
ignore line if line starts with # (comment probably)
next if ($line !~ /^[0-9].*$/m);
ignore line if it does not start with a number

Which means the previous two lines were useless, as this one
would have excluded blank lines and comments.
 
C

Chris Mattern

Gabriella said:
Could somebody give me the meaning of each line ?

while (<IN>) {

A while loop; this reads a line from file handle IN into
$_. It stops looping when there's nothing more to read
from IN.
my $line = $_;
Take the line you just read in $_ and assign it to
$line. my makes it a lexical variable (it only
exists inside the {} block.)
chomp($line);

Remove the trailing newline from $line
next if ($line =~ /^ *$/m);

Skip the rest of this loop iteration if $line is only
spaces, or empty. The /m is only meaningful if
$line contains multiple lines, and is pointless here.
next if ($line =~ /^#.*$/m);

Skip the rest of this loop interation is $line begins
with a #. The '.*$' is pointless, as is the /m.
next if ($line !~ /^[0-9].*$/m);

Skip the rest of this loop if line does not start with
a digit. Once again, the '.*$' and the /m are pointless.
#$line =~ s/#.*$//m;

Would delete everything in $line from the first # to the
end of the line, if it weren't commented out. /m is
still pointless.
$line =~ s/ +$//m;

Deletes any trailing spaces in $line. /m is still pointless.
$line =~ s/^ +//m;

Deletes any leading spaces in $line. Utterly pointless
as a previous line terminated this iteration unless $line
started with a digit.

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
C

Chris Mattern

Lars said:
In our last episode,
the lovely and said:
On 2 Mar 2005 13:35:34 -0800
(e-mail address removed) (Gabriella) wrote:
While reading input (eg file on commandline)
my $line = $_;
set $line to be the next(first) line in the file
chomp($line);
remove newline from the end of the line.
next if ($line =~ /^ *$/m);
ignore line and goto next line if line only contains spaces or is empty
next if ($line =~ /^#.*$/m);
ignore line if line starts with # (comment probably)
next if ($line !~ /^[0-9].*$/m);
ignore line if it does not start with a number

Which means the previous two lines were useless, as this one
would have excluded blank lines and comments.

Missed that one. Whoever did this isn't very good.
--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
A

Ala Qumsieh

Chris said:
Gabriella wrote:


Remove the trailing newline from $line

Or whatever is defined in $/, in case that changed.
Skip the rest of this loop iteration if $line is only
spaces, or empty. The /m is only meaningful if
$line contains multiple lines, and is pointless here.

Not necessarily. If $/ changed, then $line can contain multiple lines.
There is no way to check, but I doubt that is the case.

--Ala
 
E

Eric Schwartz

Could somebody give me the meaning of each line ?

while (<IN>) {
my $line = $_;
chomp($line);
next if ($line =~ /^ *$/m);
next if ($line =~ /^#.*$/m);
next if ($line !~ /^[0-9].*$/m);
#$line =~ s/#.*$//m;
$line =~ s/ +$//m;
$line =~ s/^ +//m;

Others have dissected it; here's my attempt at a better rewrite, so
you can see what a proper job might look like:

while(my $line = <IN>) {
chomp($line);
next unless $line =~ /^\d/;
$line =~ s/^\s+//;
$line =~ s/\s+$//;

# ... other stuff goes here
}

The main difference here is that I removed the useless regexes Lars
Eighner commented on, and I reversed the sense of the one that's left.
As a matter of personal style, I tend to avoid the !~ operator because
I feel =~ is easier to read. This isn't a hard and fast rule with me,
but I find it serves me well.

-=Eric
 
F

Fabian Pilkowski

* Eric Schwartz said:
Others have dissected it; here's my attempt at a better rewrite, so
you can see what a proper job might look like:

while(my $line = <IN>) {
chomp($line);
next unless $line =~ /^\d/;
$line =~ s/^\s+//;
$line =~ s/\s+$//;

# ... other stuff goes here
}

Do you think it's required to delete all leading white spaces from a
line starting with a number? It's also required to chomp the line, if a
newline is one of the trailing white spaces? Since you have changed the
original code to delete all trailing *white spaces* ("\s") instead of
*blanks* (" "), you can lessen your code's rewrite:

while ( my $line = <IN> ) {
next unless $line =~ /^\d/;
$line =~ s/\s+$//;

# ...
}

regards,
fabian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top