You wrote:
> open(ABC, "TEST.html") or die "Can't open TEST: $!\n";
> while ($line = <ABC>)
> {
> if ($line =~ /hello\s\d\d\d/ )
> {$hello=$&}
> if ($line =~ /E\d\d\d-\d\d\d\d/ or $line =~ /E\d\d\d\d\d\d\d/)
> {$pager=$&}
> print "$number : $hello\n";
> };
First, this won't run. You haven't defined $number anywhere, so you
couldn't possibly get the output you have listed. And it bombs
instantly if you try running it under 'use strict' as you really
ought to - but that's a side issue.
Second, your problem is that you only update $hello and $pager when
you have a match. So in every line where there is no match,
you leave $hello and $pager as they were before. If you have two
blank lines [lines with just a hard return], you have two extra
lines where that print() is going to print out what you had the
previous time. In the docs they do talk about this a little. Try
this at a command line: perldoc perlre [or use the HTML documentation
which is really quite nice] to get the bird's eye lowdown on regular
expressions.
You need to put that print() statement inside your if statements
if you only want to print out lines where there is a match. Also,
there are better approaches than using $& to capture your matches.
Try this:
#!/usr/bin/perl -w
use strict;
while (<DATA>){
print $1," : ",$2,"\n" if /(E\d{3}-\d{3}|E\d{7}).*hello (\d{3})/;
};
__DATA__
E1234567 The quick brown fox jumps over the lazy dog hello 123.
the quick brown E0404040 toad jumps over the lazy croc hello 456
the quick E0487474 brown duck jumps over the lazy cat hello 789
E040-4774 the quick brown chick jumps over the lazy pig hello 101
And see if you don't get this:
E1234567 : 123
E0404040 : 456
E0487474 : 789
E040-477 : 101
The program uses the default $_ variable instead of your $line,
and it captures the matches using the parentheses into the variables
$1 and $2 . This can be tightened up even further, but I wanted you
to see something basic and [hopefully] comprehensible.
David
--
David Cassell, OAO [EMAIL PROTECTED]
Senior computing specialist
mathematical statistician
---
You are currently subscribed to perl-win32-users as: [[email protected]]
To unsubscribe, forward this message to
[EMAIL PROTECTED]
For non-automated Mailing List support, send email to
[EMAIL PROTECTED]