 |
 |
reversing words in place using perl
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2003
Status:
Offline
|
|
I’m a beginner and I’m limited to using perl, version 5.005_01 built for powerpc-machten, I don’t have modules.
I have been trying to reverse text with each word in place.
from:
mary had a little lamb
its fleece was white as snow
to:
yram dah a elttil bmal
sti eceelf saw etihw sa wons
Can someone help?
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
My Perl is really weak, but it should work something like this: Split the string at the spaces (using split()), iterate through that, split each word by an empty string (i.e. split("", $word)) and reverse those arrays (using reverse()).
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2003
Status:
Offline
|
|
Chuckit, it's going to take me a while to figure it out so I'm going to thank you now. I think you have put me on the correct path.
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Mar 2001
Location: yes
Status:
Offline
|
|
homework assignment? 
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2003
Status:
Offline
|
|
Sorry, wrong assumption but understandable. I’m retired. I want this for my hobby. I have tried several methods to reverse the output but no luck. My latest attempt only reversed the entire text end to end. I don’t program, so I don’t get the constant practice needed to remember what I learned when I first started to teach myself Perl. I am too old now and my mental capacity isn’t what it used to be. The program I have is complex and does a lot of things and it works. It was only when I decided to modify the first portion of it (I wanted to be able to reverse the letters in the words in place), that I ran into trouble. It was an afterthought and even after Chuckit’s suggestion, I don’t see how to accomplish the reversal I want to do.
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Like I said, my Perl isn't great, so I'm sure this code is completely unidiomatic and not at all what an actual Perl guy would write, but here's an example that does something along the lines of what you want:
Code:
$text = "Mary had a little lamb";
@words = split(" ", $text);
foreach $word (@words)
{
@word = split("", $word);
$reversed = join("", reverse(@word));
print($reversed . " ");
}
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2003
Status:
Offline
|
|
Chuckit, thank you very much. It works for me.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|