Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Pattern Matching With Regular Expressions

by Paladin (Vicar)
on Apr 13, 2004 at 03:37 UTC ( [id://344606]=note: print w/replies, xml ) Need Help??


in reply to Pattern Matching With Regular Expressions

You open your file just inside the if, then read the entire file in the while loop. Then next time through the foreach $term(@inputs) { loop, there is nothing left to read so it exits the loop immediatly.

You have a few options:

  1. Reverse the while and foreach loops, so it loops through @inputs for each line read. or:
  2. Rewind the file after the while loop.
  3. If the file is small enough, read the entire thing into memory, and use a second foreach loop instead of the while

I don't know if it matters to you which order you get your matches, nor if the files are small enough, so I'm not sure which option would be better in this case.

Update: You might also want to reformat your code to make it easier to see where each block ends. Running your code through perltidy produces the following:

sub findtext { @filenumbers = @_; foreach $number (@filenumbers) { push @filenumbers2, split(/\W/, $number); } foreach $number (@numbers2) { chomp $number; if (defined $number) { open(FILE, "/home/jroberts/$number.txt") or die "$!"; foreach $term (@inputs) { while (<FILE>) if (/\b($term)\b/i) { push @before, split(' ', $`); @before = reverse(@before); @before = splice(@before, 0, 7); @before = reverse(@before); push @after, split(' ', $'); @after = splice(@after, 0, 7); if (exists $results{$number}) { $existing = $results{$number}; $results{$number} = $existing . "... @before" . "<b>$&</b>" . "@after ..."; } else { $results{$number} = "... @before" . "<b>$&</b>" . "@after "; } @before = undef; @after = undef; next; } else { print "No Match\n"; next; } } print "Match found in $number.txt\n"; @fulltext = $results{$number}; print "@fulltext\n"; close(FILE); } else { next; } next; } }

Replies are listed 'Best First'.
Re: Pattern Matching With Regular Expressions
by Anonymous Monk on Apr 13, 2004 at 03:47 UTC
    What exactly do you mean by rewind. And by reverse do you mean switch places?
    jroberts
      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 ";
      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://344606]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-23 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found