replace DATA with a proper filehandle to your file...{ my @records; local $/; # allow to read in whole file for(split /\s*\|--------------------------------------\|\s*/, <DAT +A>) { my %rec; next unless /\S/; #check for data for(split "\n", $_) { # split up the datum in a record my($key,$val) = split ':', $_, 2; # split the datum, the , +2 limits split to #only splitting the string into at most 2 pieces, +saving further : $rec{$key} = $val; #store in hash } push @records, \%rec; # store recs in array } for(@records) { #iterate and print, you get the idea print "$_->{Date}\n"; } } __DATA__ |--------------------------------------| Date: today Request: text here text here text here Name: Joe Bloggs Tel: 0123 45677 email: joebloggs@bloggs.com |--------------------------------------| Date: Today Request: text here text here text here Name: John Smith Tel: 0123 45677 email: johnsmith@smith.com |--------------------------------------|
If it is a very big file, you might want something that reads line by lines and maintains state, but this is one way to do it...
- Ant
- Some of my best work - Fish Dinner
In reply to Re: processing text from flat file
by suaveant
in thread processing text from flat file
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |