John W. Krahn wrote:
Brett Williams wrote:

Hi :)


Hello,

I am still very new to perl (and programming) and am getting stuck with regular expressions. I have a text file in which I want to find, then print to screen all lines beginning with "?" and then print the text between the "<" and ">" characters. The code I have tried (amongst many variations) is below

(open(INPUT, "record.txt")) || die("Error, can't find file\n");
$line = readline(INPUT);
while($line =~ /^\?/)
{
chomp($line);
print($line =~ /(.+)<\/a>\]/);
$line = readline(INPUT);
}
close(INPUT);


open INPUT, '<', 'record.txt' or die "Error, can't open 'record.txt' $!";

while ( <INPUT> ) {
    next unless /^\?/;
    print "$1\n" if /<([^>]+)>/;
    }

close INPUT;



John
We want some line numbers (and I like parens):
my ($line) = 1;
open (FILE, '<record.txt') or die ("Blah blah: $!");
while (<FILE>) {
        next unless m/^\?/;
        print ($line++, ": $1\n") if m/<(.*)>/;
        # Is there any reason for this not to be greedy?
}
close (FILE);

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to