To get all possible substrings of a string in Perl, you can use nested loops to iterate through each combination of start and end indices.
In this example,
$str
with a value of 'Hello'
.@substrings
to store all substrings.@substrings
array.@substrings
array.my $str = 'Hello';
my @substrings;
for my $i (0..length($str)-1) {
for my $j ($i+1..length($str)) {
push @substrings, substr($str, $i, $j-$i);
}
}
print "All substrings: @substrings\n";
All substrings: H He Hel Hell Hello e el ell ello l ll llo lo o
In this tutorial, we learned How to get all the possible Substrings of a String in Perl language with well detailed examples.