I have an input file with
words;
yadda yadda;
keyword 123;
yadda;
and I want to simply get the value 123 saved as a variable. I tried a solution from here:
my $var;
open(FILE,$data.dat) or die "error on opening $data: $!\n";
while (my $line = <FILE>) {
if (/^keyword/) {
$var = $1;
print $line;
last;
}
}
close(FILE);
This isn't working and gives me following error: Use of uninitialized value $_ in pattern match (m//) at ./script.pl line 91, <FILE> line 384.
(this occurs for all lines of <FILE>
)
I found another solution without the if-condition which just states @string = sort grep /^keyword/,<FILE>;
and works. Can you please explain to me what is happening here?
/edit
Thx for the answers and explanations! What do you think is the better/more elegant way? The grep or the if-condition?