Thursday, 28 October 2021

Top 500 unique perl programming interview questions and answers to master in perl regular expression programs - part1

 

  1. How can I remove leading and trailing white spaces from a string in Perl?
perl
1$str = " Hello World! "; 2$str =~ s/^\s+|\s+$//g; 3print $str; # Output: "Hello World!"
  1. How can I remove all occurrences of a substring from a string in Perl?
perl
1$str = "Hello, World! Hello, Universe!"; 2$str =~ s/Hello, //g; 3print $str; # Output: "World! Universe!"
  1. How can I count the number of occurrences of a substring in a string in Perl?
perl
1$str = "Hello, World! Hello, Universe!"; 2$count = () = $str =~ /Hello, /g; 3print $count; # Output: 2
  1. How can I check if a string starts with a certain substring in Perl?
perl
1$str = "Hello, World!"; 2if ($str =~ /^Hello, /) { 3 print "Yes"; 4} else { 5 print "No"; 6}
  1. How can I check if a string ends with a certain substring in Perl?
perl
1$str = "Hello, World!"; 2if ($str =~ / World!$/) { 3 print "Yes"; 4} else { 5 print "No"; 6}
  1. How can I split a string into an array of substrings based on a delimiter in Perl?
perl
1$str = "Hello, World! Hello, Universe!"; 2@words = split(/ /, $str); 3print "@words\n"; # Output: "Hello, World! Hello, Universe!"
  1. How can I replace all occurrences of a pattern in a string with a different string in Perl?
perl
1$str = "Hello, World! Hello, Universe!"; 2$str =~ s/World/Earth/g; 3print $str; # Output: "Hello, Earth! Hello, Universe!"
  1. How can I perform a case-insensitive match of a pattern in a string in Perl?
perl
1$str = "Hello, World!"; 2if ($str =~ /world/i) { 3 print "Yes"; 4} else { 5 print "No"; 6}
  1. How can I perform a case-insensitive search and replace of a pattern in a string in Perl?
perl
1$str = "Hello, World! Hello, Universe!"; 2$str =~ s/world/Earth/gi; 3print $str; # Output: "Hello, Earth! Hello, Universe!"
  1. How can I capture all occurrences of a pattern in a string in Perl?
perl
1$str = "Hello, World! Hello, Universe!"; 2@matches = $str =~ /(Hello, \w+!)/g; 3print "@matches\n"; # Output: "Hello, World! Hello, Universe!"
  1. How can I capture a substring based on a pattern in Perl?
perl
1$str = "Hello, World!"; 2if ($str =~ /Hello, (\w+)!/) { 3 print $1; # Output: "World" 4}
  1. How can I capture all occurrences of a pattern and replace them with different strings in Perl?
perl
1$str = "Hello, World! Hello, Universe!"; 2$str =~ s/(Hello, )(\w+)(!)/$1 . ($2 eq "World" ? "Earth" : "Heaven") . $3/ge; 3print $str; # Output: "Hello, Earth! Hello, Heaven!"
  1. How can I extract all occurrences of a pattern and their positions in a string in Perl?
perl
1$str = "Hello, World! Hello, Universe!"; 2@positions = grep { /Hello, (\w+)!/g } 0 .. length($str); 3print "@positions\n"; # Output: "0 14"
  1. How can I check if a string matches a pattern in Perl?
perl
1$str = "Hello, World!"; 2if ($str =~ /^Hello, \w+!$/) { 3 print "Yes"; 4} else { 5 print "No"; 6}
  1. How can I escape all special characters in a string in Perl?
perl
1$str = "Hello, World!"; 2$escaped_str = quotemeta($str); 3print $escaped_str; # Output: "Hello\, World\!"
  1. How can I perform a non-greedy match of a pattern in a string in Perl?
perl
1$str = "Hello, World! Hello, Universe!"; 2@matches = $str =~ /(Hello, \w+!).*?(?=Hello, \w+!|$)/g; 3print "@matches\n"; # Output: "Hello, World! Hello, Universe!"
  1. How can I limit the number of matches in Perl?
perl
1$str = "Hello, World! Hello, Universe!"; 2$count = 2; 3@matches = $str =~ /(Hello, \w+!)/g for 1 .. $count; 4print "@matches\n"; # Output: "Hello, World! Hello, Universe!"
  1. How can I make the 's' modifier case-insensitive in Perl?
perl
1$str = "Hello, World!"; 2$str =~ s/WORLD/Earth/gi; 3print $str; # Output: "Hello, Earth!"
  1. How can I insert a substring into a string at a specific position in Perl?
perl
1$str = "Hello, World!"; 2$position = 8; 3$str = substr($str, 0, $position) . "Earth, " . substr($str, $position); 4print $str; # Output: "Hello, Earth, World!"
  1. How can I delete a substring from a string at a specific position in Perl?
perl
1$str = "Hello, Earth, World!"; 2$position = 8; 3$length = 6; 4$str = substr($str, 0, $position) . substr($str, $position + $length); 5print $str; # Output: "Hello, World!"
  1. How can I pad a string with a specific character to a specific length in Perl?
perl
1$str = "Hello, World!"; 2$pad_length = 20; 3$pad_char = "="; 4$str = sprintf("%-*s", $pad_length, $str); 5print $str; # Output: "Hello, World!======="
  1. How can I truncate a string to a specific length in Perl?
perl
1$str = "Hello, World!"; 2$truncate_length = 13; 3$str = substr($str, 0, $truncate_length); 4print $str; # Output: "Hello, World"
  1. How can I convert a string to uppercase or lowercase in Perl?
perl
1$str = "Hello, World!"; 2$uppercase_str = uc($str); 3$lowercase_str = lc($str); 4print "$uppercase_str\n$lowercase_str\n"; # Output: "HELLO, WORLD!" "hello, world!"
  1. How can I remove leading and trailing whitespace from a string in Perl?
perl
1$str = " Hello, World! "; 2$trimmed_str = $str =~ s/^\s+|\s+$//gr; 3print $trimmed_str; # Output: "Hello, World!"
  1. How can I reverse a string in Perl?
perl
1$str = "Hello, World!"; 2$reversed_str = reverse($str); 3print $reversed_str; # Output: "!dlroW ,olleH"
  1. How can I replace all occurrences of a character with another character in a string in Perl?
perl
1$str = "Hello, World!"; 2$str =~ s/World/Earth/g; 3print $str; # Output: "Hello, Earth!"
  1. How can I replace all occurrences of a substring with another substring in a string in Perl?
perl
1$str = "Hello, World! Hello, Universe!"; 2$str =~ s/World/Earth/g; 3print $str; # Output: "Hello, Earth! Hello, Universe!"
  1. How can I join two strings with a specific separator in Perl?
perl
1$str1 = "Hello, "; 2$str2 = "World!"; 3$separator = " "; 4$joined_str = join($separator, $str1, $str2); 5print $joined_str; # Output: "Hello, World!"
  1. How can I split a string into an array based on a specific delimiter in Perl?
perl
1$str = "Hello, World! Hello, Universe!"; 2$delimiter = " "; 3@array = split(/$delimiter/, $str); 4print "@array\n"; # Output: "Hello,", "World!", "Hello,", "Universe!"
  1. How can I remove all occurrences of a character from a string in Perl?
perl
1$str = "Hello, World!"; 2$char_to_remove = "l"; 3$str =~ s/$char_to_remove//g; 4print $str; # Output: "Heo, Word!"
  1. How can I remove all occurrences of a substring from a string in Perl?
perl
1$str = "Hello, World! Hello, Universe!"; 2$substring_to_remove = "World!"; 3$str =~ s/$substring_to_remove//g; 4print $str; # Output: "Hello, Hello, Universe!"
  1. How can I find the position of a substring in a string in Perl?
perl
1$str = "Hello, World! Hello, Universe!"; 2$substring = "World!"; 3$position = index($str, $substring); 4print $position; # Output: 13
  1. How can I count the number of occurrences of a substring in a string in Perl?
perl
1$str = "Hello, World! Hello, Universe!"; 2$substring = "World!"; 3$count = () = $str =~ /$substring/g; 4print $count; # Output: 1
  1. How can I determine if a string contains a specific substring in Perl?
perl
1$str = "Hello, World! Hello, Universe!"; 2$substring = "World!"; 3if (index($str, $substring) != -1) { 4 print "The string contains the substring.\n"; 5} else { 6 print "The string does not contain the substring.\n"; 7}
  1. How can I remove leading whitespace from a string in Perl?
perl
1$str = " Hello, World! "; 2$str =~ s/^\s+//; 3print $str; # Output: "Hello, World! "
  1. How can I remove trailing whitespace from a string in Perl?
perl
1$str = " Hello, World! "; 2$str =~ s/\s+$//; 3print $str; # Output: " Hello, World!"
  1. How can I concatenate two strings in Perl?
perl
1$str1 = "Hello, "; 2$str2 = "World!"; 3$concatenated_str = $str1 . $str2; 4print $concatenated_str; # Output: "Hello, World!"
  1. How can I format a string in Perl?
perl
1$name = "Alice"; 2$age = 30; 3$formatted_str = sprintf("My name is %s and I am %d years old.", $name, $age); 4print $formatted_str; # Output: "My name is Alice and I am 30 years old."
  1. How can I pad a string in Perl?
perl
1$pad_char = "="; 2$pad_length = 20; 3$str = "Hello, World!"; 4$padded_str = sprintf("%-*s", $pad_length, $str); 5print $padded_str; # Output: "Hello, World!==========="
  1. How can I get the length of a string in Perl?
perl
1$str = "Hello, World!"; 2$length = length($str); 3print $length; # Output: 13
  1. How can I check if a string is empty in Perl?
perl
1$str = ""; 2if ($str eq "") { 3 print "The string is empty.\n"; 4} else { 5 print "The string is not empty.\n"; 6}
  1. How can I compare two strings in Perl?
perl
1$str1 = "Hello, World!"; 2$str2 = "Hello, Universe!"; 3if ($str1 eq $str2) { 4 print "The strings are equal.\n"; 5} else { 6 print "The strings are not equal.\n"; 7}
  1. How can I convert a string to lowercase in Perl?
perl
1$str = "Hello, World!"; 2$lowercase_str = lc($str); 3print $lowercase_str; # Output: "hello, world!"
  1. How can I convert a string to uppercase in Perl?
perl
1$str = "Hello, World!"; 2$uppercase_str = uc($str); 3print $uppercase_str; # Output: "HELLO, WORLD!"
  1. How can I convert a string to a number in Perl?
perl
1$str = "42"; 2$num = 0 + $str; 3print $num; # Output: 42
  1. How can I escape special characters in a string in Perl?
perl
1$str = 'Hello, World!'; 2$escaped_str = quotemeta($str); 3print $escaped_str; # Output: "Hello\,\ World\!"
  1. How can I generate a random string in Perl?
perl
1use strict; 2use warnings; 3use String::Random; 4 5my $length = 10; 6my $all_chars = 'A'..'Z'; 7my $string_random = String::Random->new($all_chars); 8my $random_string = $string_random->randstring($length); 9print $random_string; # Output: A random string of 10 uppercase letters
  1. How can I reverse a string in Perl?
perl
1$str = "Hello, World!"; 2$reversed_str = reverse($str); 3print $reversed_str; # Output: "!dlroW ,olleH"
  1. How can I remove duplicate characters from a string in Perl?
perl
1$str = "Hello, World!"; 2$no_duplicates_str = join '', sort { $a cmp $b } split //, $str; 3print $no_duplicates_str; # Output: "!HWdelor , World! H"
  1. How can I remove a substring from a string in Perl?
perl
1$str = "Hello, World!"; 2$substring = "World"; 3$str =~ s/$substring//g; 4print $str; # Output: "Hello, !"
  1. How can I count the occurrences of a substring in a string in Perl?
perl
1$str = "Hello, World!"; 2$substring = "o"; 3my $count = () = $str =~ /$substring/g; 4print $count; # Output: 2
  1. How can I split a string into an array in Perl?
perl
1$str = "Hello, World!"; 2my @words = split(/ /, $str); 3print join(", ", @words); # Output: "Hello,, World!"
  1. How can I join an array into a string in Perl?
perl
1my @words = ("Hello", ",", "World", "!"); 2$str = join(" ", @words); 3print $str; # Output: "Hello, World!"
  1. How can I iterate over each character in a string in Perl?
perl
1$str = "Hello, World!"; 2foreach my $char (split //, $str) { 3 print $char; 4} 5print "\n"; # Output: "Hello, World!"
  1. How can I iterate over each line in a string in Perl?
perl
1$str = "Hello, World!\nGoodbye, World!"; 2foreach my $line (split /\n/, $str) { 3 print $line; 4} 5print "\n"; # Output: "Hello, World!" followed by "Goodbye, World!"
  1. How can I perform string substitutions in Perl?
perl
1$str = "Hello, World!"; 2$str =~ s/World/Universe/g; 3print $str; # Output: "Hello, Universe!"
  1. How can I remove whitespace from a string in Perl?
perl
1$str = " Hello, World! "; 2$str =~ s/^\s+|\s+$//g; # Remove leading and trailing whitespace 3$str =~ s/\s+/ /g; # Replace internal multiple whitespace with a single space 4print $str; # Output: "Hello, World!"
  1. How can I perform case-insensitive string comparisons in Perl?
perl
1$str1 = "Hello, World!"; 2$str2 = "hello, world!"; 3if ($str1 eq $str2) { 4 print "The strings are equal (case-sensitive).\n"; 5} 6if ($str1 =~ m/^$str2$/i) { 7 print "The strings are equal (case-insensitive).\n"; 8}
  1. How can I search for a pattern in a string in Perl?
perl
1$str = "Hello, World!"; 2if ($str =~ m/World/) { 3 print "The pattern 'World' was found in the string.\n"; 4}
  1. How can I search for a pattern in a string and replace it in Perl?
perl
1$str = "Hello, World!"; 2$str =~ s/World/Universe/g; 3print $str; # Output: "Hello, Universe!"
  1. How can I extract a substring from a string in Perl?
perl
1$str = "Hello, World!"; 2$substring = substr($str, 0, 5); 3print $substring; # Output: "Hello"
  1. How can I check if a string starts with a specific substring in Perl?
perl
1$str = "Hello, World!"; 2if ($str =~ m/^Hello/) { 3 print "The string starts with 'Hello'.\n"; 4}
  1. How can I check if a string ends with a specific substring in Perl?
perl
1$str = "Hello, World!"; 2if ($str =~ m/World!$/) { 3 print "The string ends with 'World!'.\n"; 4}
  1. How can I reverse a string in Perl?
perl
1$str = "Hello, World!"; 2$reversed_str = reverse($str); 3print $reversed_str; # Output: "!dlroW ,olleH"
  1. How can I escape special characters in a string for use in a regular expression in Perl?
perl
1$str = "Hello, World!"; 2$escaped_str = quotemeta($str); 3print $escaped_str; # Output: "Hello,\\ World\!"
  1. How can I match the first occurrence of a pattern in a string in Perl?
perl
1$str = "Hello, World!"; 2if ($str =~ m/(World)/ && defined $1) { 3 print "The first occurrence of the pattern 'World' is: $1\n"; 4}
  1. How can I match the last occurrence of a pattern in a string in Perl?
perl
1$str = "Hello, World!"; 2if ($str =~ m/(World)\b(?!.*\b$1\b)/ && defined $1) { 3 print "The last occurrence of the pattern 'World' is: $1\n"; 4}
  1. How can I count the number of lines in a string in Perl?
perl
1$str = "Hello, World!\nGoodbye, World!"; 2my $count = () = $str =~ /\n/g; 3print $count; # Output: 1
  1. How can I replace a character at a specific position in a string in Perl?
perl
1$str = "Hello, World!"; 2substr($str, 5, 1) = ", "; 3print $str; # Output: "Hello, World!"
  1. How can I replace all occurrences of a character in a string in Perl?
perl
1$str = "Hello, World!"; 2$str =~ s/,/ /g; 3print $str; # Output: "Hello World!"
  1. How can I insert a string at a specific position in a string in Perl?
perl
1$str = "Hello, World!"; 2substr($str, 5, 0) = ", "; 3print $str; # Output: "Hello, World!"
  1. How can I split a string into an array of substrings based on a delimiter in Perl?
perl
1$str = "Hello, World!"; 2my @substrings = split(/, /, $str); 3print "@substrings\n"; # Output: "Hello World!"
  1. How can I join an array of strings into a single string with a delimiter in Perl?
perl
1my @strings = ("Hello", "World!"); 2my $joined_str = join(", ", @strings); 3print $joined_str; # Output: "Hello, World!"
  1. How can I sort an array of strings in Perl?
perl
1my @strings = ("World", "Hello"); 2@strings = sort @strings; 3print "@strings\n"; # Output: "Hello World"
  1. How can I find the length of a string in Perl?
perl
1$str = "Hello, World!"; 2my $length = length($str); 3print $length; # Output: 13
  1. How can I remove duplicates from an array in Perl?
perl
1my @strings = ("Hello", "World", "Hello"); 2my %seen; 3@strings = grep { ! $seen{$_}++ } @strings; 4print "@strings\n"; # Output: "Hello World"
  1. How can I iterate over each character in a string in Perl?
perl
1$str = "Hello, World!"; 2for my $char (split //, $str) { 3 print "$char\n"; 4}
  1. How can I iterate over each substring in a string based on a delimiter in Perl?
perl
1$str = "Hello, World!"; 2my @substrings = split(/, /, $str); 3for my $substring (@substrings) { 4 print "$substring\n"; 5}
  1. How can I compare two strings in Perl?
perl
1$str1 = "Hello"; 2$str2 = "World"; 3if ($str1 eq $str2) { 4 print "The strings are equal.\n"; 5} else { 6 print "The strings are not equal.\n"; 7}
  1. How can I perform case-insensitive string comparison in Perl?
perl
1$str1 = "Hello"; 2$str2 = "hello"; 3if ($str1 eq $str2) { 4 print "The strings are equal.\n"; 5} else { 6 print "The strings are not equal.\n"; 7}
  1. How can I check if a string is empty in Perl?
perl
1$str = ""; 2if ($str eq "") { 3 print "The string is empty.\n"; 4} else { 5 print "The string is not empty.\n"; 6}
  1. How can I remove leading and trailing whitespace from a string in Perl?
perl
1$str = " Hello, World! "; 2$str =~ s/^\s+|\s+$//g; 3print $str; # Output: "Hello, World!"
  1. How can I strip all occurrences of leading and trailing whitespace from substrings in a string in Perl?
perl
1$str = " Hello, World! "; 2my @substrings = split(/\s+/, $str); 3print "@substrings\n"; # Output: "Hello, World!"
  1. How can I strip all occurrences of non-word characters from a string in Perl?
perl
1$str = "Hello, World!"; 2$str =~ s/\W+//g; 3print $str; # Output: "HelloWorld"
  1. How can I strip all occurrences of non-word characters from substrings in a string in Perl?
perl
1$str = "Hello, World!"; 2my @substrings = split(/\W+/, $str); 3print "@substrings\n"; # Output: "Hello World"
  1. How can I strip all occurrences of leading non-word characters from a string in Perl?
perl
1$str = ",Hello, World!"; 2$str =~ s/^\W+//; 3print $str; # Output: "Hello, World!"
  1. How can I strip all occurrences of trailing non-word characters from a string in Perl?
perl
1$str = "Hello, World!,"; 2$str =~ s/\W+$//; 3print $str; # Output: "Hello, World!"
  1. How can I count the number of occurrences of a substring in a string in Perl?
perl
1$str = "Hello, World!"; 2my $count = () = $str =~ /World/g; 3print $count; # Output: 1
  1. How can I find the index of the first occurrence of a substring in a string in Perl?
perl
1$str = "Hello, World!"; 2my $index = index($str, "World"); 3print $index; # Output: 7
  1. How can I find the index of the last occurrence of a substring in a string in Perl?
perl
1$str = "Hello, World!"; 2my $index = rindex($str, "World"); 3print $index; # Output: 7
  1. How can I escape all special characters in a string in Perl?
perl
1$str = "Hello, World!"; 2$str =~ s/([\$\@\\])/\\$1/g; 3print $str; # Output: "Hello\,\ World\!"
  1. How can I replace all occurrences of a substring in a string in Perl?
perl
1$str = "Hello, World!"; 2$str =~ s/World/Universe/g; 3print $str; # Output: "Hello, Universe!"
  1. How can I split a string into lines in Perl?
perl
1$str = "Hello, World!\nHello, Universe!"; 2my @lines = split(/\n/, $str); 3print "@lines\n"; # Output: "Hello, World! Hello, Universe!"
  1. How can I split a string into words in Perl?
perl
1$str = "Hello, World!"; 2my @words = split(/\s+/, $str); 3print "@words\n"; # Output: "Hello, World!"
  1. How can I convert a string to lowercase in Perl?
perl
1$str = "Hello, World!"; 2$str = lc($str); 3print $str; # Output: "hello, world!"
  1. How can I convert a string to uppercase in Perl?
perl
1$str = "Hello, World!"; 2$str = uc($str); 3print $str; # Output: "HELLO, WORLD!"
  1. How can I reverse a string in Perl?
perl
1$str = "Hello, World!"; 2$str = reverse($str); 3print $str; # Output: "!dlroW ,olleH"
  1. How can I convert a string to an integer in Perl?
perl
1$str = "42"; 2$num = int($str); 3print $num; # Output: 42
  1. How can I convert a string to a floating-point number in Perl?
perl
1$str = "42.5"; 2$num = $str + 0; 3print $num; # Output: 42.5
  1. How can I concatenate two strings in Perl?
perl
1$str1 = "Hello, "; 2$str2 = "World!"; 3$str = $str1 . $str2; 4print $str; # Output: "Hello, World!"
  1. How can I concatenate multiple strings in Perl?
perl
1$str = join("", "Hello, ", "World!", "!"); 2print $str; # Output: "Hello, World!!!"
  1. How can I get the length of a string in Perl?
perl
1$str = "Hello, World!"; 2$len = length($str); 3print $len; # Output: 13
  1. How can I trim whitespace from the beginning and end of a string in Perl?
perl
1$str = " Hello, World! "; 2$str =~ s/^\s+|\s+$//g; 3print $str; # Output: "Hello, World!"
  1. How can I substitute the first occurrence of a substring in a string in Perl?
perl
1$str = "Hello, World!"; 2$str =~ s/World/Universe/; 3print $str; # Output: "Hello, Universe!"
  1. How can I pad a string to a certain length with spaces in Perl?
perl
1$str = "Hello, World!"; 2$str = sprintf("%-20s", $str); 3print $str; # Output: "Hello, World! "
  1. How can I create a range of strings in Perl?
perl
1@strings = ('a'..'c'); 2print "@strings\n"; # Output: "a b c"
  1. How can I generate a random string in Perl?
perl
1use List::Util qw(shuffle); 2my @chars = ('a'..'z', 'A'..'Z', 0..9); 3my $random_string = join('', shuffle(@chars)[0..7]); 4print $random_string; # Output: Randomly generated string of 8 characters
  1. How can I convert a string to a hexadecimal representation in Perl?
perl
1$str = "Hello, World!"; 2$hex = unpack("H*", $str); 3print $hex; # Output: "48656c6c6f2c20576f726c6421"
  1. How can I convert a string from a hexadecimal representation in Perl?
perl
1$hex = "48656c6c6f2c20576f726c6421"; 2$str = pack("H*", $hex); 3print $str; # Output: "Hello, World!"
  1. How can I remove duplicate words from a string in Perl?
perl
1$str = "Hello, World! Hello, Universe!"; 2my %seen; 3my @words = grep { ! $seen{$_}++ } split; 4$str = join(" ", @words); 5print $str; # Output: "Hello, World! Universe!"
  1. How can I remove duplicate lines from a string in Perl?
perl
1$str = "Hello, World! 2Hello, Universe! 3Hello, World!"; 4my %seen; 5my @lines = grep { ! $seen{$_}++ } split /\n/, $str; 6$str = join("\n", @lines); 7print $str; # Output: "Hello, World! 8Hello, Universe!"
  1. How can I sort lines in a string in Perl?
perl
1$str = "Hello, World! 2Hello, Universe! 3Hello, Earth!"; 4my @lines = sort split /\n/, $str; 5$str = join("\n", @lines); 6print $str; # Output: "Hello, Earth! 7Hello, Universe! 8Hello, World!"
  1. How can I remove leading and trailing spaces from each line in a string in Perl?
perl
1$str = " Hello, World! 2 Hello, Universe! 3 Hello, Earth! "; 4my @lines = map { s/^\s+|\s+$//g; $_ } split /\n/, $str; 5$str = join("\n", @lines); 6print $str; # Output: "Hello, World! 7Hello, Universe! 8Hello, Earth!"
  1. How can I split a string into paragraphs in Perl?
perl
1$str = "Hello, World! 2 3Hello, Universe! 4 5Hello, Earth!"; 6my @paragraphs = split /\n{2,}/, $str; 7print "@paragraphs\n"; # Output: "Hello, World! 8Hello, Universe! 9Hello, Earth!"
  1. How can I reverse a string in Perl?
perl
1$str = "Hello, World!"; 2$str = reverse $str; 3print $str; # Output: "!dlroW ,olleH"
  1. How can I escape special characters in a string in Perl?
perl
1$str = "Hello, World!"; 2$str =~ s/([\^$.*+?()[\\]{}|\\])/\\$1/g; 3print $str; # Output: "Hello\,\ World\!"
  1. How can I replace non-alphabetic characters with spaces in a string in Perl?
perl
1$str = "Hello, World!"; 2$str =~ s/[^a-zA-Z]/./g; 3print $str; # Output: "Hello World "

Labels:

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home