my %hash; while (<>) { $hash{$_}++; } # %hash is accessible outside the while() loop print join ', ' keys %hash;