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"; } } #### my $found_logout = 1; my $i = 0; my %hash; while ($i < @tokens) { if ($found_logout) { %hash = (date => $tokens[$i++]); $found_logout = 0; } else { my $key = $tokens[$i++]; my $value = $tokens[$i++]; $hash{$key} = $value; if ($key eq "Logout") { $found_logout = 1; do_db_stuff(\%hash); } } } sub do_db_stuff { print Dumper shift; }