my $prev; while (defined my $data = $sth->fetchrow_hashref) { my $this = $data->{id}; ... $prev = $data; };