in reply to Splitting a file into records

A small variation on davido's proposal. Instead of storing the records in a hash I am using an array of hashes. Every time I encounter a new "STORE" line, I push a new hash reference onto the array. This way I do not need to store the store in a variable, as index -1 always refers to the last element. Just for completeness I put the header lines into the same structure.

In order to make things look nicer, I also assumed that the lines of your data are not split but come from something like ls -l. Should this assumption be incorrect one has to change the third regex to m/^[dlrwx-]{10}|^\d+/. One could also merge the first and third regex into one as the action is identical but the code borders on obfuscation already...

use strict; use warnings; use Data::Dumper; my @records = ( { store => 'header' } ); while ( my $line = <DATA> ) { chomp $line; push @{$records[-1]{lines}}, $line if $line =~ m/^STORE|^Scan/; push @records, { store => $1 } if $line =~ m/^\s(STORE\d{3})/; push @{$records[-1]{lines}}, $line if $line =~ m/^[-dlrwx]{10}/; + } print Dumper \@records; __DATA__ STORE MONITORING REPORT as of 13-05-02 10:05:07 Scanning for FTFIMS STORE002 -rwxr-xr-- 1 admins store 59025 Apr 11 2012 eft100.cbr 16295 58 -rwxr-xr-- 1 admins store 61143 Nov 15 15:47 chk075.cbr 33334 60 -rwxr-xr-- 1 admins store 420952 Sep 6 2012 test-encrypt 63327 41 +2 -rwxr-xr-- 1 admins store 427068 Sep 6 2012 eft115-20 36184 418 -rwxr-xr-- 1 admins store 460694 Apr 3 06:15 eft6un 07640 450 -rwxrwxrwx 1 admins store 481069 Oct 4 2012 hostsocgw 46087 470 -rwxrwxrwx 1 admins store 503666 Feb 13 09:10 stratgw 22452 492 -rwxr-xr-- 1 admins store 14318 Nov 1 2010 unityrep 50196 14 STORE006 -rwxr-xr-- 1 admins store 59025 Apr 11 2012 eft100.cbr 16295 58 -rwxr-xr-- 1 admins store 61143 Nov 15 15:47 chk075.cbr 33334 60 -rwxr-xr-- 1 admins store 420952 Sep 6 2012 test-encrypt 63327 41 +2 -rwxr-xr-- 1 admins store 427068 Sep 6 2012 eft115-20 36184 418 -rwxr-xr-- 1 admins store 460694 Apr 3 06:15 eft6un 07640 450 -rwxrwxrwx 1 admins store 481069 Oct 4 2012 hostsocgw 46087 470 -rwxrwxrwx 1 admins store 503666 Feb 13 09:10 stratgw 22452 492 -rwxr-xr-- 1 admins store 14318 Nov 1 2010 unityrep 50196 14