-
-
Save smartree/af0a38140fdf2a1613e8aac590f9cf91 to your computer and use it in GitHub Desktop.
3 ways to loop through each character in a string in Perl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 3 ways to loop through each character in a string | |
| $text = "hello world"; | |
| for $i (0..length($text)-1){ | |
| $char = substr($text, $i, 1); | |
| print "Index: $i, Text: $char \n"; | |
| } | |
| foreach $char (split //, $text) { | |
| print "$char\n"; | |
| } | |
| foreach $char (split('', $text)) { | |
| print "$char\n"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment