Sei sulla pagina 1di 2

1.

Develop a Perl script that sorts lines of the given file in reverse order, so the last line becomes
first and vice-versa. The script must have two command line arguments, the first is an input file
name and the second is an output file name. For input file check to have existing and readable
file, otherwise print error and exit program. For output file check to have non-existing file,
otherwise print error and exit.

CODE:

$in=@ARGV[0];
$out=@ARGV[1];
open IN,"<$in" or die "Error Opening Input file: $!";
while (<IN>)
{
push (@content, $_);
}
close (IN);

print "Original Lines:\n";


print "@content\n";
$j=0;
for ($i=$#content; $i>=0; $i--)
{

$new_content[$j]=$content[$i];
$j++;
}
print "\n";

print "Reversed Lines:\n";


open OUT, ">$out" or die "Error opening the file. $!";
print OUT @new_content;
print @new_content;
close (OUT);

OUTPUT: