in reply to pushing similar lines into arrays

You'll probably want to use a hash of arrays, keyed by the initial string -- something like this:
my %lines; while (<>) { my ($key) = ( /^(\S+)/ ); # captures first token, assigns that to +$key push @{$lines{$key}}, $_; # (update: fixed missing "}") } for (sort keys %lines) { my $nlines = scalar @{$lines{$_}}; print "$nlines lines keyed by $_ :\n"; print " First line is: $lines{$_}[0]"; print " Last line is: $lines{$_}[$nlines-1]"; }