Results 1 to 2 of 2

Thread: selecting between records

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 1999
    Location
    London, England
    Posts
    213

    selecting between records

    Using perl, I want to pick up only the records between Start and Finish in INFILE:

    i.e

    Start
    AAH
    BBH
    CCH
    Finish

    I am using the following code but this only excludes Start and writes everything else to output.txt ?

    my $sField = "Start";

    foreach $_ (<INFILE> ) {
    if ($_ !~ $sField) {
    print OUTFILE;
    }
    }

  2. #2
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Try this, I think it's a little clearer as to what's going on:
    Code:
    use strict;
    
    my $infile = 'file.txt';
    my $found = 0;
    
    open(INFILE, $infile) or die "Can't open $infile: $!\n\n";
    
    foreach my $line (<INFILE>) {
    	if ($line =~ /Start/) {
    		$found = 1;
    	} elsif ($line =~ /Finish/) {
    		$found = 0;
    	} elsif ($found == 1) {
    		print OUTFILE $line;
    	}
    	
    }
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

Posting Permissions

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



Click Here to Expand Forum to Full Width