#!/usr/bin/perl -w use strict; while (<>) { my @pos; while (m/\s+/g) { # Loop while it matches push @pos, pos(); } print join " ", @pos; print "\n"; }