Skip to content

Instantly share code, notes, and snippets.

@smartree
Forked from ishu3101/loop_string.pl
Created December 16, 2019 06:15
Show Gist options
  • Save smartree/af0a38140fdf2a1613e8aac590f9cf91 to your computer and use it in GitHub Desktop.
Save smartree/af0a38140fdf2a1613e8aac590f9cf91 to your computer and use it in GitHub Desktop.

Revisions

  1. @ishu3101 ishu3101 created this gist Jun 7, 2016.
    16 changes: 16 additions & 0 deletions loop_string.pl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    # 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";
    }