use strict; use warnings; my $file = 'data.txt'; open my $fh, '<', $file or die "failed to open $file: $!"; my @tokens = split /,/, <$fh>; close $fh; chomp @tokens; my $found_logout = 1; my $date; my $i = 0; while ($i < @tokens) { if ($found_logout) { $date = $tokens[$i++]; $found_logout = 0; print "*****************************\n"; print "New date: $date\n"; } else { my $key = $tokens[$i++]; my $value = $tokens[$i++]; print "$key = $value\n"; # do DB stuff with $key and $value $found_logout = 1 if $key eq "Logout"; } }