I am looping through multi-line records and loading them into an array. I happen to be using Perl , but language is irrelevant as I am looking for an optimization to the algorithm. Specifically, I am bothered by the fact that I am writing the array push twice. Once in the loop when I find an end of record (eor) and again when I run out of file (eof, not eor). I know this doesn't affect the speed, I just don't like have to repeat the code in two places. It means that if it changes, I have to modify in two places.
The approach I am taking is this:
my $data = []; #data object array
my $record = {};
my $line;
while (my $line = <$file>){
if($line =~ /marker-a:(.*)/){
# Update data object
$$record{'a'} = $1;
}
if($line =~ /marker-b:(.*)/){
# Update data object
$$record{'b'} = $1;
}
if($line =~ /eor/){
# End of record; add to data array
push(@$data,$record);
$record = {};
}
}
#Update leftover data to data array
push(@$data,$record);
Is there a better way to do this? I know I could just create a function, but I am looking for something more elegant. I haven't tested this code, but it should give enough of an idea of what I am doing. Let me know if any questions.
All that is necessary is to change
to
and remove the
push
outside the loopUpdate
Here's a more complete solution that uses best practices and avoids pushing empty records onto the array