use warnings; use strict; my $chap; my $sect; while () { if (/^(\d+)/) { $chap = $1; $sect = 0; } else { $sect++; s/^/$chap.$sect /; } print; } __DATA__ 1. Operators Performing simple arithmetic Operating on bitwise values Comparing values Operating on Boolean values Operating on parts of a container with the slice operator Understanding operator precedence 2. Regular Expressions Using the re module Searching with regular expressions