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! |
Oneliner of the day
Code:
perl -e 'while(<>) {push @_,split /(\s+)/} print reverse @_' < raven.txt > nevar_eth.txt |
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>" Code:
#!/usr/bin/perl Chris |
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 |
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 |
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" }' |
Hmmmmmmmmmmmm... Lemme throw that into a perl script and see what I come up with. I will let you know. Dinnertime. bbl. Thanks!
|
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!
Chris |
I wrote:
Code:
#!/usr/bin/perl Code:
This is is a test to see if this this will work. Chris |
You're forgetting to actually READ the file :-)
Hint: You probably want a "while" loop, like some of the examples in this thread. |
Ok, I see what you mean. I did it again:
Code:
#!/usr/bin/perl A duplicate word "" was found What is this ""? Thanks! |
You've got the print statment outside the scope of the while (it needs to be inside the curly braces somewhere).
|
Ok,
I simply changed this: Code:
} Code:
print "A duplicate word \"$1\" was found\n"; 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! |
I don't think you've been working through the Programming Perl book ;-)
Try this: Code:
#!/usr/bin/perl |
All times are GMT -5. The time now is 10:11 AM. |