stupid line matching question 
Author Message
 stupid line matching question

A while ago, someone wrote me a perl script I use for sending mail and
news that sticks on a sig and calls my editor on the tmp file.  nn puts
headers in this tmp file, and I want to skip them.  This is what the
original function did, taking advantage of the blank line that always
shows up after the headers (SIGFILE is the tmp file):

if ($file =~ /nn/)
{
  open(SIGFILE, "$file");

  for ($[..$#msg)
  {
    $line = $_, last if $msg[$_] =~ /^$/;
  }
  close SIGFILE;
  $line++;
  $line++;
  system("vi +$line $file");

Quote:
}

I started playing with perl, though, and I wanted to change the function
so it's more memory efficient and doesn't read in the whole file.  (I
know this is kind of anal, but why not optimize?)  I wrote the following
replacement, but it never matches a blank line.

if ($file =~ /nn/)
{
  open(SIGFILE, "$file");
  $line = 0;
  $flag = 0;
  while (<SIGFILE> && ($flag == 0))
  {
    $line++;
    if (/^$/)
    {
      $flag = 1;
    }
  }
  close SIGFILE;
  $line++;
  $line++;
  system("vi +$line $file");

Quote:
}

Why doesn't it match?  What am I doing wrong?  I know I'm going to feel
stupid when I hear the answer.

--
I like work:  it fascinates me.  I can sit and look at it for hours.  I love
to keep it by me:  the idea of getting rid of it nearly breaks my heart.
                                        --Jerome K. Jerome
                                         _Three Men in a Boat_



Mon, 17 Jun 1996 08:28:21 GMT  
 stupid line matching question

:  while (<SIGFILE> && ($flag == 0))
:  {
:    $line++;
:    if (/^$/)
:    {
:      $flag = 1;
:    }
:  }

:Why doesn't it match?  What am I doing wrong?  I know I'm going to feel
:stupid when I hear the answer.

The manual says:

    If (and only if) the input symbol is the only thing inside the
    conditional of a while loop, the value is automatically assigned to
    the variable "$_".

You didn't do that.

--tom
--

      "Will Hack Perl for Fine Food and Fun"
        Boulder Colorado  303-444-3212



Mon, 17 Jun 1996 10:25:13 GMT  
 stupid line matching question
So, if something else IS included in the while loop conditional, where is
the input line stored (if not in $_) and how do you operate on the input
line or how can you then get the input line into  $_ ?
Is there some $varname such that you can say:

        $_ = $varname   # after the input that did not load $_ automatically

Sincerely
Henry Manaster

--
***************************************************************************

        Brooklyn, NY            *
                                *
        Disclaimer: The above is not necessarily MY opinion nor that
                                of anyone else :-)  ????!
****************************************************************************



Tue, 18 Jun 1996 03:27:39 GMT  
 stupid line matching question

:So, if something else IS included in the while loop conditional, where is
:the input line stored (if not in $_) and how do you operate on the input
:line or how can you then get the input line into  $_ ?
:Is there some $varname such that you can say:
:
:       $_ = $varname   # after the input that did not load $_ automatically

It's not stored at all.  It's tossed.  You have to be explicit:

    while ( ($_ = <FILE>) && $n < 50) {

    }

--tom
--

      "Will Hack Perl for Fine Food and Fun"
        Boulder Colorado  303-444-3212



Tue, 18 Jun 1996 04:36:34 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Stupid question about pattern matching

2. (stupid) pattern matching question

3. Stupid Question: scripts won't recognize opening line

4. Regexp matching at end of line question

5. Multiple Line Match Parsing Question

6. Newbie question (very stupid question I suppose)

7. Pattern bug matching whitespace in multi-line match?

8. Matching empty line plus string on next line

9. printing lines following the line with a match

10. reading lines from one file and pulling matching lines in another

11. Finding matching line based on surrounding lines

12. printing lines BEFORE a designated matching line ?

 

 
Powered by phpBB® Forum Software