251. Q: In Perl, how can you find the position of a substring within a string?
1A: In Perl, you can find the position of a substring within a string by using the `index` function. Here is an example:
2
3```perl
4use strict;
5use warnings;
6
7my $string = 'Hello, World! World is beautiful.';
8my $substring = 'World';
9
10my $position = index $string, $substring;
11
12if ($position == -1) {
13 print "The substring was not found in the string.\n";
14} else {
15 print "The substring was found at position: $position\n";
16}
17```
18
19In this example, the script finds the position of the substring `'World'` within the string `'Hello, World! World is beautiful.'` and prints the resulting position.
Read more »Labels: Perl Interview Questions and Answers - 2023 Updated