ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!
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;
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.
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 :-))
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!
#!/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!
#!/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;
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;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.