in reply to Replacing, cutting, deleting lines in the file
A simple code snippet
#!/usr/bin/perl -w use strict; use warnings; use constant HANDLER => { 'line something starts here {' => sub{ return "Handler 1: do s +omethig with this data\n".shift;}, 'next block of something starts here {' => sub{ return "Handle +r 2: do somethig with this data\n".shift;}, # Other record Handlers }; # Read Records while (my $line = <DATA>){ chomp $line; next unless $line =~ /{/; my $header = $line; # READ HEADER # READ BODY $/="ends here }\n"; my $body = <DATA>; chomp $body; $/="\n"; print "Get Handler for: [$header]\n"; my $output = &{HANDLER->{"$header"}}($body); print $output if $output; } __DATA__ line something starts here { contains line 1 contains line 2 contains line 3 contains line 4 ends here } next block of something starts here { contains line 1 contains line 2 contains line 3 contains line 4 ends here }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Replacing, cutting, deleting lines in the file
by Jarek (Novice) on Nov 24, 2009 at 14:38 UTC | |
by gulden (Monk) on Nov 24, 2009 at 18:42 UTC | |
by Jarek (Novice) on Nov 25, 2009 at 09:08 UTC |