D
divyajacob
I was trying out a program
Accept a filename as command line argument. Display the
contents of that file in the opposite order that they appear in the
file.
file content is :-
Three Rings for the Elven-kings under the sky,
Seven for the Dwarf-lords in their halls of stone,
Nine for Mortal Men doomed to die,
One for the Dark Lord on his dark throne
In the Land of Mordor where the Shadows lie.
One Ring to rule them all, One Ring to find them,
One Ring to bring them all and in the darkness bind them
In the Land of Mordor where the Shadows lie.
I want to print it like:-
In the Land of Mordor where the Shadows lie.
One Ring to bring them all and in the darkness bind them
One Ring to rule them all, One Ring to find them,
In the Land of Mordor where the Shadows lie.
One for the Dark Lord on his dark throne
Nine for Mortal Men doomed to die,
Seven for the Dwarf-lords in their halls of stone,
Three Rings for the Elven-kings under the sky,
The program I wrote is
use strict;
my $num = $#ARGV + 1;
my $str;
my @file;
if($num >= 1)
{
$str = $ARGV[0];
}
else
{
print "No argument provided\n";
}
open (FILE,$str);
while(<FILE>)
{
push(@file,$_);
}
print "Reversed file content\n";
foreach(@file)
{
print pop(@file);
}
O/p
perl ex_04.pl ringfile.txt
Reversed file content
In the Land of Mordor where the Shadows lie.
One Ring to bring them all and in the darkness bind them
One Ring to rule them all, One Ring to find them,
In the Land of Mordor where the Shadows lie.
I am getting only 4 lines in the output,Why I am not getting full 8
lines in the output?
Please help..am I doing anything wrong.
Thanks in advance
Divya
Accept a filename as command line argument. Display the
contents of that file in the opposite order that they appear in the
file.
file content is :-
Three Rings for the Elven-kings under the sky,
Seven for the Dwarf-lords in their halls of stone,
Nine for Mortal Men doomed to die,
One for the Dark Lord on his dark throne
In the Land of Mordor where the Shadows lie.
One Ring to rule them all, One Ring to find them,
One Ring to bring them all and in the darkness bind them
In the Land of Mordor where the Shadows lie.
I want to print it like:-
In the Land of Mordor where the Shadows lie.
One Ring to bring them all and in the darkness bind them
One Ring to rule them all, One Ring to find them,
In the Land of Mordor where the Shadows lie.
One for the Dark Lord on his dark throne
Nine for Mortal Men doomed to die,
Seven for the Dwarf-lords in their halls of stone,
Three Rings for the Elven-kings under the sky,
The program I wrote is
use strict;
my $num = $#ARGV + 1;
my $str;
my @file;
if($num >= 1)
{
$str = $ARGV[0];
}
else
{
print "No argument provided\n";
}
open (FILE,$str);
while(<FILE>)
{
push(@file,$_);
}
print "Reversed file content\n";
foreach(@file)
{
print pop(@file);
}
O/p
perl ex_04.pl ringfile.txt
Reversed file content
In the Land of Mordor where the Shadows lie.
One Ring to bring them all and in the darkness bind them
One Ring to rule them all, One Ring to find them,
In the Land of Mordor where the Shadows lie.
I am getting only 4 lines in the output,Why I am not getting full 8
lines in the output?
Please help..am I doing anything wrong.
Thanks in advance
Divya