johndoe3344
Programmer
Hi, I'm a newbie to perl. I just started learning it a few days ago. The short script that I made is:
The file that it takes as input, sample.txt is something like as follows:
What I want the script to do is to display only the numbers containing "KEYWORD" so for this case, I just want it to display 2, and the corresponding text, but not 1 or 3.
But my script doesn't work? Why?
#!/usr/bin/perl
use strict;
use warnings;
my $input = "sample.txt";
open (INPUT, $input) || die "File not found.\n";
@array = ();
while (my $input == INPUT) {
if (my $expression =~ /^\d+\t/) {
#matches at the beginning of the line for any number followed by a tab
push (@array, $expression);
} else {
$expression .= $expression;
}
}
unless ($expression =~ /KEYWORD/) {
print "$expression \n";
}
The file that it takes as input, sample.txt is something like as follows:
1 TEXTasdfasdf
TEXTasdfasdfasdf
2 TEXTasdfasdfasdf
asdfasdfasd KEYWORD asdfasdf
3 asdfasdfasdfasdf
adfasdfasdf
What I want the script to do is to display only the numbers containing "KEYWORD" so for this case, I just want it to display 2, and the corresponding text, but not 1 or 3.
But my script doesn't work? Why?