#!usr/bin/perl use strict; use warnings; use Data::Dumper; my %section_lines; while (my $line = ) { next if $line =~ /^\s*$/; # skip blank lines next if $line =~ /^\s*#/; # skip comments chomp $line; if ($line =~ m/\{([^}]*)\}/) { process_section($1); } else { # Could be in a "root" un-named section? Or # Could be some junk, not a comment, not a line within # a section? Shouldn't happen, but maybe it does? print STDERR "Skipping Illegal line: \'$line\'\n"; } } sub process_section { my $section_name = shift; # allow for a blank section data (no lines within it) # the existence of such a thing could have meaning? $section_lines{$section_name} = [] if (!$section_lines{$section_name}); while (my $line = ) { next if $line =~ /^\s*$/; # skip blank lines ?? next if $line =~ /^\s*#/; # skip comments ?? if ($line =~ m/\{([^}]*)\}/) # new section detected... { process_section($1); } else { $line =~ s/^\s*//; # trim leading space $line =~ s/\s*$//; # trim trailing space (inc EOL) # I have no idea of what processing is needed here. # This just adds a line to the section that is # being parsed. push @{$section_lines{$section_name}}, $line; } } } print Dumper \%section_lines; =This Program Prints: Skipping Illegal line: 'this a bogus line, not in a named section' Skipping Illegal line: 'is there a "root" un-named section possible?' $VAR1 = { 'section 2' => [], 'section 3' => [ 'xyzzy = 57', 'this line might mean something?' ], 'section 1' => [ 'a =2', 'b =something' ] }; =cut __DATA__ # Please show a "real" file here, just a guess... # this is comment this a bogus line, not in a named section is there a "root" un-named section possible? {section 1} a =2 b =something {section 2} # some comment embedded in section {section 3} # comment xyzzy = 57 this line might mean something?