You don't need to read the whole file in, but could try somehting like this:

Script starts next line:
#!perl -w
my $counter = 1;
my %headinghash;

while (<DATA>) {
    chomp;
    if ( /^(\d+)\.\s+(.*)/ ) {
       $counter = $1;
       $Headinghash{$counter} = $2 . "\n";    
     }elsif ( ! /${counter}\./ ) {
       $Headinghash{$counter} .= $_ . "\n";
     }else {
       $counter = '';
     }
=head1
        if ($file =~ /^($counter)\.\s(.*?)$/) {
                $headinghash{$counter} = $2;
                #missing code here, need data up to next occurrence of "$counter.1"
                $counter++;
        }
=cut
}
foreach my $MyKey (sort {$a <=> $b} keys %Headinghash) {
   printf "%2d:\n%-s", $MyKey, $Headinghash{$MyKey};
 }
 
__DATA__
1. This is a heading
........
1.1. A subheading
1.2. Another subheading
2. Another heading
.......
3. Yet another heading
......
^----- script ends here

Output:
 1:
This is a heading
........
 2:
Another heading
.......
 3:
Yet another heading
......

-----Original Message-----
From: birgit kellner [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, November 13, 2001 12:56
To: [EMAIL PROTECTED]
Subject: regexp


I have a file structured like this:

1. This is a heading
........
1.1. A subheading
1.2. Another subheading
2. Another heading
.......
3. Yet another heading
......

I want to extract (a) each heading number with the matching heading text, 
and (b) the immediately following text up to, and not including, the first 
subheading.

I got the first part, but not the second:
my $counter = 1;
my %headinghash;
open (FILE, "<test.txt") || die ("can't: $!");
while (<FILE>) {
        my $file = $_;
        chomp($file);
        if ($file =~ /^($counter)\.\s(.*?)$/) {
                $headinghash{$counter} = $2;
                #missing code here, need data up to next occurrence of "$counter.1"
                $counter++;
        }
}
close (FILE);

I need the regexp to take off at that point where there was a match for the 
heading, and then extract everything from there up to the occurrence of 
"$counter.1". Would this necessarily involve slurping the whole file into 
an array first?


Thanks in advance for any advice,

Birgit Kellner



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to