#!/usr/bin/perl -w use strict; #%GROUP ALSMS_consolidation #%CHPTR ALSMS_consolidation #%SBCH1 WORK_switch_id my (%outer, $group, $chapter); my @handler = ( SBCH1 => sub { my ($key, $value); print "$key: ", join(", ", @$value), "\n\n" while ($key, $value) = each %outer %outer=(); }, GROUP => sub { $group = $_[1] }, CHPTR => sub { $chapter = $_[1] }, "" => sub { push @{$outer{$_[0]}}, join ' ', @_[1 .. $#line] }, ); my %handler = @handler; my $rx = do { my @key; while(@handler) { # take two element from front, throw away the second my ($key) = splice @handler, 0, 2; push @key, $key; } join '|', @key; }; $rx = qr/^\%($rx)/; print "group ^ chapter ^ name ^ description\n"; while(<>) { if(/$rx/) { chomp; $handler{$1}->(split /\s+/); } }