PDA

View Full Version : How to exit the paragraph reading mode ?


megankong
03-14-2006, 05:30 PM
Hi,

I am trying to reading two files, both are sectioned by blank line. I have used the following code to set the reading in paragraph mode:

print "Please enter the file name: \n";
$filename1 = <STDIN>;
unless(open(DATAFILE1, $filename1)){
die ("Could not open file $filename1\n");
exit;
}

$/ = ""; set the paragraph reading mode
while(my $rec1 = <DATAFILE1>) {
push @tmp1, $rec1;
}


##However, after this reading I can no longer reading the
##file name from the command line, why?

print "Please enter the file name: \n";
$filename2 = <STDIN>;

## the program will come to a halt after I enter the second
## file name, I think I need to exit the paragraph reading ##mode, but HOW?
unless(open(DATAFILE2, $filename2)){
die ("Could not open file $filename2\n");
exit;
}

while(my $rec2 = <DATAFILE2>) {
push @tmp2, $rec2;
}

exit;

Thank you

FishMonger
03-14-2006, 06:54 PM
Try this:
#!/usr/bin/perl

use warnings;
use strict;

my (@tmp1, @tmp2);

print "Please enter the file name: ";
chomp(my $filename1 = <STDIN>);

print "Please enter the next file name: ";
chomp(my $filename2 = <STDIN>);

open(DATAFILE1, $filename1) || die ("Could not open file $filename1\n $!");
open(DATAFILE2, $filename2) || die ("Could not open file $filename2\n $!");

{
local $/ = "\n\n";
@tmp1 = <DATAFILE1>;
@tmp2 = <DATAFILE2>;
}

close DATAFILE1;
close DATAFILE2;