my @agent_ids; # outside logfile reading loop ... if (...) { ... push @agent_ids, $agent_id; }