Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to exit the paragraph reading mode ?

    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

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,838
    Thanks
    2
    Thanked 160 Times in 155 Posts
    Try this:
    Code:
    #!/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;


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •