in reply to File read and re-ordering

Like davorg and shmem, I am not sure what it is you are trying to do. Perhaps you could supply a data sample as well as the code you have tried so far and your expected output.

I have had a stab at my best guess making the assumption that your data, although possibly hundreds of records, is still small enough to fit in memory. Thus I am "slurping" the entire data file into memory. If your file is too large this may not be an option. I have annotated the code with (hopefully) explanatory comments. My example data uses just three header lines to illustrate the code in order to save space. Here it is

use strict; use warnings; # Set up start and end record sentinels. # my $startSentinel = q{**start record**}; my $endSentinel = q{**end record**}; # Compile regex to pull out records. Note the # \Q ... \E to quote regex metacharacters if your # record start and stop sentinels contain them. # my $rxExtractRecord = qr {(?xms) \Q$startSentinel\E\n (.*?) (?=\Q$endSentinel\E\n|\z) }; # Slurp file into string; I'm using the <DATA> # filehandle but you would open your file and # slurp that. # my $completeFile; { local $/; $completeFile = <DATA> } # Do a global match against compiled regex to # pull out records and put them in an array. # my @records = $completeFile =~ m{$rxExtractRecord}g; # Process each record in a loop. # foreach my $record (@records) { # Split record up into 4 items on newline. I # have used 4 here as I have only put three # headers in my data for brevity. Get the data # part by pop'ing the last item off the @items # array so that @items only contains the # headers # my @items = split m{\n}, $record, 4; my $data = pop @items # This is where your specification becomes a # bit vague. Perhaps you would pull the name out # of the hdr1:... line and open two files, e.g. # fred.hdr and fred.dat and do a printf of the # headers to the first and a print of the data # to the second. You would open, print and close # the files in this loop. Perhaps your mention # of assignment means you want to transform the # headers in some way before do a printf. # ..... do something we can't guess here ..... } __END__ **start record** hdr1:fred hdr2:2002-10-15 hdr3:head honcho and here is some date about fred telling us all what a great guy he is **end record** **start record** hdr1:pete hdr2:2005-03-22 hdr3:bottle washer not much data for pete **end record** **start record** hdr1:mary hdr2:2004-01-31 hdr3:personal assistant mary is a great asset to the company and will go far **end record**

Hopefully this and the other responses will get you started.

Cheers,

JohnGG

Update: Used variables to hold start and end sentinels.