Let us learn Perl the practical way with some examples that will help to understand the usage of different commands and constructs in Perl.
Example 1: Given an array of numbers, write a Perl code to remove the elements that are divisible by 2 and print the array.
Logic:
- Traverse each element of the array using foreach (for loop)
- If the element is not divisible by 2, push to a new array
- Copy the new array to the old one and print it.
Perl Code:
my $element; my @array = (1,2,3,4,5,6); foreach $element (@array) { if ($element % 2 != 0) {push(@result, $element);} } @array = @result; print "@array\n";
Example 2: Perform the above example without the use of an additional array variable.
Logic:
- Get the length of the array using $#array + 1 ($# is a special variable)
- Have an index variable i going from 0 to length-1 to traverse the array
- Get the first element of array at index 0.
- If the element is not divisible by 2, push the element to the end of the same array.
- Shift the array one step to the left, so that the next element falls at index 0.
- Increment i. Goto step 3 until while loop completes (when i goes beyond original array length).
- When loop completes, all our original elements would have been shifted out and we are left with only the newly pushed elements.
- Print the array.
Perl Code:
my $i; my $element; my @array = (1,2,3,4,5,6); my $len = $#array + 1; while ($i < $len) { $element = $array[0]; if ($element % 2 != 0) {push(@array, $element);} shift (@array); $i++; } print "@array\n";
Example 3: Consider a paragraph with many round brackets ( ). Write a Perl code to do the following:
If any line contains either an open or closed bracket, remove it.
If the line contains both the brackets, do nothing.
Logic:
- Split the paragraph into lines by using split at '. '
- Use while loop to traverse each line.
- Use variable curline to store the current line.
- If curline contains either '(' or ')' , basically xor, then we remove the bracket by performing substitution with nothing. Note the use of backslash for special characters like brackets.
- Then we use join function to join each line and get back the paragraph.
- Print the modified paragraph.
Perl Code:
my $para = "This is the para (here) line 1. This is (line 2."; my $i = 0; my @line = split (/\. /, $para); $lines = $#line + 1; while ($i < $lines) { $curline = $line[$i]; if ($curline =~ m/\(/ xor $curline =~ m/\)/) {$curline =~ s/\(//g; $curline =~ s/\)//g;} if ($i > 0) {$para = join (". ", $para, $curline);} else {$para = $curline;} $i++; } print "$para\n";
Example 4: Perform the above example with these modifications:
Get the paragraph from user instead of having it in code.
If the line contains a closed bracket followed by an open bracket, invert them to make it open followed by closed.
If the line contains an open bracket followed by a closed bracket, do nothing.
If the line contains an open bracket followed by a closed bracket, do nothing.
Assume that no line contains more than one set of open and closed brackets.
Logic:
This will be similar to the above code, changes have been added in comments.
Note the trick in the last substitution.
Since g is omitted, substitution will be done only at the first occurence of ) to ensure that the previous substitution is not overwritten.
Perl Code:
print "Enter the paragraph: "; my $para = <STDIN>; #accept input chomp($para); #remove newline my $i = 0; my @line = split (/\. /, $para); $lines = $#line + 1; while ($i < $lines) { $curline = $line[$i]; if ($curline =~ m/\(/ xor $curline =~ m/\)/) {$curline =~ s/\(//g; $curline =~ s/\)//g;} if ($curline =~ m/\(/ and $curline =~ m/\)/) #condition to invert brackets if both present { $index1 = index($curline,'('); $index2 = index($curline,')'); if ($index1 > $index2) {$curline =~ s/\(/\)/g; $curline =~ s/\)/\(/;} #no g for second substitution } if ($i > 0) {$para = join (". ", $para, $curline);} else {$para = $curline;} $i++; } print "$para\n";
No comments:
Post a Comment