We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Easy way to reverse file read in perl?

dr34m3rs
dr34m3rs asked
on
Medium Priority
677 Views
Last Modified: 2012-05-06
I would like to know if there is an easy way to read a file in reverse.

In other words, read from the end of the file to the start of the file instead of start to end. I feel it would be more efficient rather than loading it into an array and then reversing it in memory, to just load it already reversed.
open(R,"$my_file");
# read file in reverse with <R>
close(R);

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
print reverse <R>;


or
open my $rev, '<:reverse', $file;

or

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
or

use File::ReadBackwards ;
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
or  
 use Tie::File;
masheikSoftware Engineer
CERTIFIED EXPERT

Commented:
hi, try this code
open( FILE, "<$file_to_reverse" )
  or die( "Can't open file file_to_reverse: $!" );
 
@lines = reverse <FILE>;
 
foreach $line (@lines)
 {
  # do something with $line
 }

Open in new window

CERTIFIED EXPERT

Commented:

Author

Commented:
Thanks!

Author

Commented:
Perfect, thanks.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.