LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-30-2003, 12:13 PM   #1
WorldBuilder
Member
 
Registered: Mar 2003
Location: Massachusetts, USA
Distribution: RH 8, Solaris, Windoze eXPunged
Posts: 520

Rep: Reputation: 30
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!
 
Old 10-30-2003, 01:56 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Oneliner of the day
Code:
perl -e 'while(<>) {push @_,split /(\s+)/} print reverse @_' < raven.txt > nevar_eth.txt
 
Old 10-30-2003, 02:11 PM   #3
WorldBuilder
Member
 
Registered: Mar 2003
Location: Massachusetts, USA
Distribution: RH 8, Solaris, Windoze eXPunged
Posts: 520

Original Poster
Rep: Reputation: 30
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
 
Old 10-30-2003, 02:31 PM   #4
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
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;
 
Old 10-30-2003, 02:42 PM   #5
WorldBuilder
Member
 
Registered: Mar 2003
Location: Massachusetts, USA
Distribution: RH 8, Solaris, Windoze eXPunged
Posts: 520

Original Poster
Rep: Reputation: 30
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

Last edited by WorldBuilder; 10-30-2003 at 02:50 PM.
 
Old 10-30-2003, 03:54 PM   #6
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
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" }'
 
Old 10-30-2003, 04:45 PM   #7
WorldBuilder
Member
 
Registered: Mar 2003
Location: Massachusetts, USA
Distribution: RH 8, Solaris, Windoze eXPunged
Posts: 520

Original Poster
Rep: Reputation: 30
Hmmmmmmmmmmmm... Lemme throw that into a perl script and see what I come up with. I will let you know. Dinnertime. bbl. Thanks!
 
Old 10-30-2003, 07:05 PM   #8
Nightblade_oz
LQ Newbie
 
Registered: Aug 2003
Location: Australia
Distribution: Fedora Core 2
Posts: 24

Rep: Reputation: 15
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 :-))
 
Old 10-30-2003, 07:12 PM   #9
WorldBuilder
Member
 
Registered: Mar 2003
Location: Massachusetts, USA
Distribution: RH 8, Solaris, Windoze eXPunged
Posts: 520

Original Poster
Rep: Reputation: 30
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
 
Old 10-30-2003, 07:45 PM   #10
WorldBuilder
Member
 
Registered: Mar 2003
Location: Massachusetts, USA
Distribution: RH 8, Solaris, Windoze eXPunged
Posts: 520

Original Poster
Rep: Reputation: 30
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
 
Old 10-30-2003, 08:05 PM   #11
Nightblade_oz
LQ Newbie
 
Registered: Aug 2003
Location: Australia
Distribution: Fedora Core 2
Posts: 24

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

Hint: You probably want a "while" loop, like some of the examples in this thread.
 
Old 10-30-2003, 08:21 PM   #12
WorldBuilder
Member
 
Registered: Mar 2003
Location: Massachusetts, USA
Distribution: RH 8, Solaris, Windoze eXPunged
Posts: 520

Original Poster
Rep: Reputation: 30
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!
 
Old 10-30-2003, 08:50 PM   #13
Nightblade_oz
LQ Newbie
 
Registered: Aug 2003
Location: Australia
Distribution: Fedora Core 2
Posts: 24

Rep: Reputation: 15
You've got the print statment outside the scope of the while (it needs to be inside the curly braces somewhere).
 
Old 10-30-2003, 08:56 PM   #14
WorldBuilder
Member
 
Registered: Mar 2003
Location: Massachusetts, USA
Distribution: RH 8, Solaris, Windoze eXPunged
Posts: 520

Original Poster
Rep: Reputation: 30
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!

Last edited by WorldBuilder; 10-30-2003 at 08:59 PM.
 
Old 10-30-2003, 09:08 PM   #15
Nightblade_oz
LQ Newbie
 
Registered: Aug 2003
Location: Australia
Distribution: Fedora Core 2
Posts: 24

Rep: Reputation: 15
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;
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create log file from script fotoguy Programming 12 03-27-2012 06:39 AM
perl script help required to create users and changing the owner to pericular folder sridhar11 Programming 2 10-25-2005 02:16 PM
How can I create KDE/Gnome menu shorcut via perl script? mf1234 Programming 1 08-16-2005 05:46 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
Looking for simple way to create scheduled emails from perl script kdowney Programming 1 10-17-2003 06:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration