in reply to Re: Pattern Matching With Regular Expressions
in thread Pattern Matching With Regular Expressions

What exactly do you mean by rewind. And by reverse do you mean switch places?
jroberts
  • Comment on Re: Pattern Matching With Regular Expressions

Replies are listed 'Best First'.
Re: Re: Pattern Matching With Regular Expressions
by Thelonius (Priest) on Apr 13, 2004 at 04:16 UTC
    By rewind he means seek FILE, 0, 0, so that the next read is at the beginning of the file.

    And by reverse the loops, he means change:

    foreach $term (@inputs) { while (<FILE>) {
    to
    while (<FILE>) { foreach $term (@inputs) {

    I'm going to add a few unrelated points:
    Always:

    use warnings; use strict;
    In fact, you probably have a bug of "filenumbers2" vs. "numbers2" that use strict would catch.

    Instead of

    push @before, split(' ', $`);
    you should have
    @before = split(' ', $`);
    Then you don't need the awkward @before = undef; The same holds for @after, too.

    Really it should be

    my @before = split /\s+/, $`;
    The three lines
    @before = reverse(@before); @before = splice(@before, 0, 7); @before = reverse(@before);
    are better written as splice(@before, 0, -7);. And this code:
    if(exists $results{$number}) { $existing = $results{$number}; $results{$number} = $existing . "... @before" . "<b>$&</b>" . "@afte +r ..."; } else { $results{$number} = "... @before" . "<b>$&</b>" . "@after "; }
    can be written as
    $results{$number} .= "... @before" . "<b>$&</b>" . "@after ";
Re: Re: Pattern Matching With Regular Expressions
by graff (Chancellor) on Apr 13, 2004 at 04:13 UTC
    Sometimes we forget that perl doesn't actually have a function called "rewind" -- but it does have a function called seek, which achieves the same result. The issue is that a normal file handle maintains a "pointer" to the next position to be read (i.e. the end-point of the data that was last read). When you get to the end of the file, this pointer is at EOF, which means nothing else can be read.

    The "seek" function (perldoc -f seek) can reposition the pointer to any position in the file; a typical place to go is back to the beginning:

    seek FILE, 0, 0; # position file pointer at start of file