LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   perl script to create a backwards file?! (https://www.linuxquestions.org/questions/programming-9/perl-script-to-create-a-backwards-file-110446/)

WorldBuilder 10-30-2003 12:13 PM

perl script to create a backwards file?!
 
Hi all,

I am trying to learn perl on my own. Since I'm not even remotely programming adept, it's proving to be rather difficult. I hate programming, but I think it would be a useful skill nonetheless!

What I'm trying to do right now is write a perl program that reads in a text file (I have a text file of The Raven by Edgar Allen Poe, called raven.txt) and then creates another file, called nevar_eht.txt which is a complete reversal of the original file. I want it to start at the very end of raven.txt and create a completely backward version, word by word (not letter by letter). I have been reading my books over and over and am getting nowhere. Google searches are not proving to be very fruitful, either. Would somone be so gracious as to point me in the right direction? Anyone got any ideas on how to write this out? I don't even know where to begin! Thank you all!

ToniT 10-30-2003 01:56 PM

Oneliner of the day
Code:

perl -e 'while(<>) {push @_,split /(\s+)/} print reverse @_' < raven.txt > nevar_eth.txt
;)

WorldBuilder 10-30-2003 02:11 PM

That's cool. I put that into an actual script, but am getting an eror from line 6.
Code:

syntax error at 10.pl line 6, near "txt>"
Here's my code:
Code:

#!/usr/bin/perl
open (FILE, 'raven.txt') || die ("failed to open :$!");
        while(<FILE> ) {
                push @_,split /(\s+)/
        }
        print reverse @_ <raven.txt> "nevar_eth.txt";

What am I missing? Thank you!

Chris

ToniT 10-30-2003 02:31 PM

My example accessed stdin and stdout and made the shell to do the forwarding.

The following does it independently (modified version of your code)
Code:

#!/usr/bin/perl -w
open (FILE, 'raven.txt') || die ("failed to open :$!");
open (OUTFILE, '>nevar_eth.txt') || die ("Failed to open target file");
while(<FILE>) {
    push @_,split /(\s+)/;
}
print OUTFILE reverse @_;
close FILE;
close OUTFILE;


WorldBuilder 10-30-2003 02:42 PM

Oh, I see it. You had to open the newly named file. Ok.Thanks a million!

I got another one for ya that is troubling me. I have been thinking about this one, too.

Suppose I wanted to write a perl script that reads from a file and detects lines that have double words, that is, two words that are exactly the same separated only by blank spaces. For example, a line of the file might read:

In my opinion, the the solution is to use perl.

Notice the 2 "the"s? I want to catch things like this as a possible typo by reusing a previously matched pattern in the regex. Assuming the current line being processed is stored in a variable called $_, how would I write write the regular expression that will return true when double words are encountered? This one completely escapes me!

Chris

ToniT 10-30-2003 03:54 PM

A sample oneliner.
Code:

echo 'In my opinion, the the solution is to use perl.' | perl -ne 'if(/(\S+) \1 /){ print "A duplicate word \"$1\" found\n" }'

WorldBuilder 10-30-2003 04:45 PM

Hmmmmmmmmmmmm... Lemme throw that into a perl script and see what I come up with. I will let you know. Dinnertime. bbl. Thanks!

Nightblade_oz 10-30-2003 07:05 PM

Might I suggest you try an on-line Perl tutorial? They often cover these sorts of file manipulations in their examples.

And if you find you're *really* getting into it, I would recommend you purchase a good book, like "Programming Perl" from O'Reilly (I call it the Perl Bible :-))

WorldBuilder 10-30-2003 07:12 PM

That's one of the books I have! It is good. It's one of those things that I simply am not good at. You know how some people are mechanical and don't know squat about, say... electricity? Same thing. I'm really good with computer hardware, server or workstation. For some reason, I have a very hard time wrapping my brain around programming languages. Strange... I speak 2 languages!

Chris

WorldBuilder 10-30-2003 07:45 PM

I wrote:
Code:

#!/usr/bin/perl
use strict;
open (FILE, 'textfile') || die ("failed to open :$!");
        if (/(\S+) \1 /) {
        print "A duplicate word \"$1\" was found\n"
        }
close FILE;

The file, textfile, looks like this and is in the same directory:
Code:

This is is a test to see if this this will work.
When I run the perl script, it simply returns to a command prompt. What am I missing, guys? Thanks!

Chris

Nightblade_oz 10-30-2003 08:05 PM

You're forgetting to actually READ the file :-)

Hint: You probably want a "while" loop, like some of the examples in this thread.

WorldBuilder 10-30-2003 08:21 PM

Ok, I see what you mean. I did it again:
Code:

#!/usr/bin/perl
use strict;
open (FILE, 'textfile') || die ("failed to open :$!");
        while (<FILE> ) {
        (/(\S+) \1 /)
        }
        print "A duplicate word \"$1\" was found\n";
close FILE;

Now, when I run the script it says:

A duplicate word "" was found

What is this ""?

Thanks!

Nightblade_oz 10-30-2003 08:50 PM

You've got the print statment outside the scope of the while (it needs to be inside the curly braces somewhere).

WorldBuilder 10-30-2003 08:56 PM

Ok,

I simply changed this:
Code:

        }
        print "A duplicate word \"$1\" was found\n";

to this:
Code:

        print "A duplicate word \"$1\" was found\n";
        }

Now I get an error saying:
syntax error at double_words.pl line 6, near ")
print"
Execution of double_words.pl aborted due to compilation errors.

Where in AU are you from? Been to Sydney. Beautiful!

Nightblade_oz 10-30-2003 09:08 PM

I don't think you've been working through the Programming Perl book ;-)

Try this:

Code:

#!/usr/bin/perl

use warnings;

open( FILE, 'textfile' ) || die ( "failed to open :$!" );
while ( <FILE> )
{
    if ( /(\S+) \1 / )
    {
        print "Duplicate word \"$1\" was found\n";
    }
}
close FILE;



All times are GMT -5. The time now is 10:11 AM.