perl commands

Discussion in 'Perl Misc' started by Gabriella, Mar 2, 2005.

  1. Gabriella

    Gabriella Guest

    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
    Gabriella, Mar 2, 2005
    #1
    1. Advertising

  2. Gabriella

    Kim Schulz Guest

    On 2 Mar 2005 13:35:34 -0800
    (Gabriella) wrote:

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



    --
    Kim Schulz | Need a Content Management System for your website? Go
    Geek by nature | get Fundanemt at : http://www.fundanemt.com New
    schulz.dk | version out now!
    Kim Schulz, Mar 2, 2005
    #2
    1. Advertising

  3. Gabriella

    Jim Keenan Guest

    Gabriella wrote:
    > 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.
    Jim Keenan, Mar 2, 2005
    #3
  4. Gabriella

    Lars Eighner Guest

    In our last episode,
    <>, the lovely and
    talented Kim Schulz broadcast on comp.lang.perl.misc:

    > On 2 Mar 2005 13:35:34 -0800
    > (Gabriella) wrote:


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


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

    >> #$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
    >> Thanks


    --
    Lars Eighner http://www.io.com/~eighner/
    "Shhh! Be vewwy, vewwy quiet! I'm hunting Muswims!"
    - President Elmer Bush
    Lars Eighner, Mar 2, 2005
    #4
  5. Gabriella wrote:

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

    > Thanks


    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
    Chris Mattern, Mar 2, 2005
    #5
  6. Lars Eighner wrote:

    > In our last episode,
    > <>, the lovely and
    > talented Kim Schulz broadcast on comp.lang.perl.misc:
    >
    >> On 2 Mar 2005 13:35:34 -0800
    >> (Gabriella) wrote:

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

    >
    > 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.
    >
    >>> #$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
    >>> Thanks

    >


    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
    Chris Mattern, Mar 2, 2005
    #6
  7. Chris Mattern <> wrote:

    > Whoever did this isn't very good.



    That sums it up quite well.

    It clearly was written my an amateur.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 2, 2005
    #7
  8. Gabriella

    Ala Qumsieh Guest

    Chris Mattern wrote:
    > Gabriella wrote:


    >> chomp($line);

    >
    >
    > Remove the trailing newline from $line


    Or whatever is defined in $/, in case that changed.

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


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

    --Ala
    Ala Qumsieh, Mar 2, 2005
    #8
  9. (Gabriella) writes:
    > 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
    --
    Come to think of it, there are already a million monkeys on a million
    typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.
    Eric Schwartz, Mar 3, 2005
    #9
  10. * Eric Schwartz wrote:
    >
    > 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
    Fabian Pilkowski, Mar 3, 2005
    #10
  11. Gabriella

    Anno Siegel Guest

    Tad McClellan <> wrote in comp.lang.perl.misc:
    > Chris Mattern <> wrote:
    >
    > > Whoever did this isn't very good.

    >
    >
    > That sums it up quite well.
    >
    > It clearly was written my an amateur.


    Hey! Don't give amateurs a bad name!

    Anno
    Anno Siegel, Mar 3, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Pfaff

    Re: man pages for C commands (GCC commands)

    Ben Pfaff, Jun 24, 2003, in forum: C Programming
    Replies:
    4
    Views:
    3,966
    Thomas Stegen
    Jun 28, 2003
  2. Tim Stanka
    Replies:
    1
    Views:
    789
    Jeff Epler
    Aug 2, 2004
  3. Anand
    Replies:
    2
    Views:
    555
    Roedy Green
    May 20, 2008
  4. \Dandy\ Randy

    Help needed with basic Perl math commands

    \Dandy\ Randy, Jul 27, 2003, in forum: Perl Misc
    Replies:
    4
    Views:
    150
    David H. Adler
    Jul 27, 2003
  5. Nick
    Replies:
    4
    Views:
    131
    C. Olive
    Sep 23, 2003
Loading...

Share This Page