CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Reversing the contents of a file

 



Mordanthanus
Novice

Oct 26, 2009, 9:50 PM

Post #1 of 7 (4041 views)
Reversing the contents of a file Can't Post

I have searched and searched through what feels like hundreds of forums looking for a perl script that asks for an input file name, reverses the entire file letter by letter, then outputs to another file. I am not a perl programmer by any means. I have found several things that come close... seems everyone just wants to reverse a log file. I need to reverse not by line, but by character...

Ex.

.niht dna gnol sti ,leets morf edam drows A

to

A sword made from steel, its long and thin.

I am wanting to reverse several files... the largest being 3MB.

Thanks for listening and any help is GREATLY appreciated...


shest75
Novice

Oct 27, 2009, 11:26 AM

Post #2 of 7 (4023 views)
Re: [Mordanthanus] Reversing the contents of a file [In reply to] Can't Post

Bonjour,

Please try ( you should change file1.txt by the name of your file.

open FILE, "file1.txt" or die $!;
while (<FILE>) {
print reverse;
print scalar reverse;print "\n";
}


(This post was edited by shest75 on Oct 27, 2009, 11:31 AM)


Mordanthanus
Novice

Oct 27, 2009, 12:57 PM

Post #3 of 7 (4016 views)
Re: [shest75] Reversing the contents of a file [In reply to] Can't Post

After running that in a command window, it just scrolls the file... locking up the command window. Could you tell me how to pipe that into another txt file?


shest75
Novice

Oct 27, 2009, 1:10 PM

Post #4 of 7 (4014 views)
Re: [Mordanthanus] Reversing the contents of a file [In reply to] Can't Post

hum...

perl your_file.pl > result.txt

or :

open FILE, "file1.txt" or die $!;
open my $out, '>', "result.txt" or die "Can't write new file: $!";

while (<FILE>) {
print reverse;
print $out scalar reverse;print $out "\n";
}

close $out;


Mordanthanus
Novice

Oct 27, 2009, 1:37 PM

Post #5 of 7 (4010 views)
Re: [shest75] Reversing the contents of a file [In reply to] Can't Post

Ok, I'm an idiot... I didn't know I could ">" that into another file... lol. Thanks very much shest.


omatza
Novice

Nov 1, 2009, 7:21 AM

Post #6 of 7 (3966 views)
Re: [Mordanthanus] Reversing the contents of a file [In reply to] Can't Post

Mordanthanus


Can you tell, what is needed in order to have the input and output files as parameters (not hard coded) ?

Or - if I do cat input.txt | perl mordanthanusProg > outoput.txt, what should be changed in the code you gave ?

Thanks a lot


shest75
Novice

Nov 1, 2009, 9:52 AM

Post #7 of 7 (3957 views)
Re: [omatza] Reversing the contents of a file [In reply to] Can't Post

Bonjour,
Updated program :

open FILE, $ARGV[0] or die $!;
open my $out, '>', $ARGV[1] or die "Can't write new file: $!";

while (<FILE>) {
print reverse;
print $out scalar reverse;print $out "\n";
}

close $out;

then perl myfile.pl in.txt out.txt

the out.txt file will be created each time. If you want to append data at the end of the file use :

open FILE, $ARGV[0] or die $!;
open my $out, '>>', $ARGV[1] or die "Can't write new file: $!";

while (<FILE>) {
print reverse;
print $out scalar reverse;print $out "\n";
}

close $out;

Bye


(This post was edited by shest75 on Nov 1, 2009, 9:55 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives