foreach my $char (split //, $some_string) { if($char eq " ") { print "it was a space!"; # change all spaces to |s $char = "|"; } }