I would like to match a pattern from the following data set and print the line containing that pattern plus the immediate next line. Say I am interested in matching the pattern- FrNum: 1. If my program runs correctly for the following data set, the output should be :
GOP: 6 FrNum: 1 FrTyp: I GOP: 6 FrNum: 10 FrTyp: P .
GOP: 67 FrNum: 1 FrTyp: I GOP: 67 FrNum: 9 FrTyp: B
GOP: 3 FrNum: 7 FrTyp: P GOP: 3 FrNum: 14 FrTyp: B . GOP: 6 FrNum: 2 FrTyp: B GOP: 6 FrNum: 6 FrTyp: B . GOP: 6 FrNum: 1 FrTyp: I GOP: 6 FrNum: 10 FrTyp: P . . GOP: 31 FrNum: 8 FrTyp: B GOP: 31 FrNum: 11 FrTyp: B . GOP: 32 FrNum: 8 FrTyp: B GOP: 32 FrNum: 10 FrTyp: P . GOP: 36 FrNum: 2 FrTyp: B GOP: 36 FrNum: 13 FrTyp: P . GOP: 60 FrNum: 4 FrTyp: P GOP: 60 FrNum: 5 FrTyp: B . GOP: 67 FrNum: 1 FrTyp: I GOP: 67 FrNum: 9 FrTyp: B
I am new to perl and so far the code I have written is not working. What changes do I need to make?
print "Please Enter Input File\n\n";
$input_file = <STDIN>;
open(INFILE, $input_file) or die "Can't open $input_file: $!";
if($_ =~ /GOP:\s([0-9]+)\tFrNum:\s([0-9]+)\tFrTyp:\s([A-Z])/)
$_ = $next1;
if ($3 == 1)
Tell us what you mean by not working. Do you get a syntax or runtime error, or does it print too many lines or nothing at all?
Give us an example that's easy to test. You don't want to have to type the name of the file every time you test your script, do you? Once you know the file-prompting portion of your script works, set it aside and make a testable snippet of the record-matching and printing part of the script so you can test it repeatedly and debug it.
I see at least one problem in your script:
my $next1;#Declare variable
#Do other things
#$next1 still has no value
$_ = $next1;#Wipe out the current record by assigning no value. Why?
print "$_";#Print nothing?
print "$next1.\n";#Print nothing followed by period followed by newline?