Skip to content

Instantly share code, notes, and snippets.

@psimoesSsimoes
Forked from ishu3101/loop_string.pl
Created January 3, 2018 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psimoesSsimoes/6e44bae5e23bb55b57a9126eb7547584 to your computer and use it in GitHub Desktop.
Save psimoesSsimoes/6e44bae5e23bb55b57a9126eb7547584 to your computer and use it in GitHub Desktop.
3 ways to loop through each character in a string in Perl
# 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