Please I need this to be done in PERL language only
Write a program that prompts the user for a filename, then reads that file in and displays the contents backwards, line by line, and character-by character on each line. You can do this with scalars, but an array is much easier to work with. If the original file is:
abcdef
ghijkl
the output will be:
lkjihg
fedcba
AND Modify the previous script to use a die message if the filename doesn't exist. You may add a custom message if you wish.
ONLY PERL
use strict;
use warnings;
my $file = 'new.txt';
open my $info, $file or die "Could not open $file: $!";
my @arr=(); #empty array
while(my $line = <$info>) {
push(@arr,$line); #push new lines to the array
}
close $info;
my @rev_arr = reverse(@arr); #reverse the lines
print "\n The reversed file elements are : \n\n";
for my $i (0 .. $#rev_arr)
{
print scalar reverse("$rev_arr[$i]"), "\n"; #reverse the characters
}
Output:
Get Answers For Free
Most questions answered within 1 hours.