Next | Program Repair Shop | 291 |
http://perlmonks.plover.com/index.pl?node_id=141138 :
my $section = 0; my @sectone; my @secttwo; while(<TEXTFILE>) { if (/;section1/) { $section = 1; } elsif (/;section2/) { $section = 2; } else { if ($section == 1) { push(@sectionone, $_); } elsif ($section == 2) { push(@sectiontwo, $_); } else { print "No section defined for: $_\n"; } } }
Better:
my $section = 0; my @sects; while(<TEXTFILE>) { if (/;section([12])/) { $section = $1; } elsif ($section) { push @{$sects[$section]}, $_; } else { print "No section defined for: $_\n"; } }
Next | Copyright © 2002 M. J. Dominus |