sample/fresh/fresh/0/pineapple #### my $element = sample/fresh/fresh/0/pineapple; my $word = $element; while (<>) { foreach $word (m/(\w+)/g) { # parse the line and retrieve the last element. $element = $word; #I'd like to display the $element in a message box here to see the result here. } }