From: Justin C on
On 2010-06-14, divyajacob <divyajacobpulickal(a)gmail.com> wrote:
> 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);
> }

You're removing from the stack that your reading from. The 'foreach'
works with each line, you then go and confuse it by taking a line away
with 'pop'. In this instance I'd be inclined to 'while (@file)' instead.
Your pop will then be OK.

As well as 'use strict' add 'use warnings', they're very helpful.

Justin.

--
Justin C, by the sea.
From: Uri Guttman on
>>>>> "d" == divyajacob <divyajacobpulickal(a)gmail.com> writes:

just to stick my $.02 in with all the other good answers cause this is
shorter and likely faster.

d> open (FILE,$str);


use File::ReadBackwards ; # get it from cpan

tie( *HANDLE, 'File::ReadBackwards', $str ) ;
print <HANDLE> ;

uri

--
Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: John W. Krahn on
divyajacob wrote:
> 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.

#!/usr/bin/perl
use warnings;
use strict;

my $str = shift or die "No argument provided\n";

print `tac $str`;

__END__




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
From: Uri Guttman on
>>>>> "JWK" == John W Krahn <jwkrahn(a)example.com> writes:

JWK> my $str = shift or die "No argument provided\n";

JWK> print `tac $str`;

well, if you are going to fork out, why need the backticks and print?

system "tac $str" ;

uri

--
Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: John W. Krahn on
Uri Guttman wrote:
>>>>>> "JWK" == John W Krahn<jwkrahn(a)example.com> writes:
>
> JWK> my $str = shift or die "No argument provided\n";
>
> JWK> print `tac $str`;
>
> well, if you are going to fork out, why need the backticks and print?
>
> system "tac $str" ;

Cause then I would have to do all the system error checking (which you
neglected to do) and besides, print() with no filehandle does not
necessarily go to STDOUT. :-)



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein