Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > reversing words in place using perl

reversing words in place using perl
Thread Tools
Forum Regular
Join Date: Oct 2003
Status: Offline
Reply With Quote
Oct 22, 2006, 10:39 PM
 
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
Reply With Quote
Oct 23, 2006, 01:06 AM
 
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
Reply With Quote
Oct 23, 2006, 05:15 PM
 
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
Reply With Quote
Oct 23, 2006, 05:36 PM
 
homework assignment?
     
Forum Regular
Join Date: Oct 2003
Status: Offline
Reply With Quote
Oct 23, 2006, 10:21 PM
 
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
Reply With Quote
Oct 24, 2006, 09:37 AM
 
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
Reply With Quote
Oct 24, 2006, 03:37 PM
 
Chuckit, thank you very much. It works for me.
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 08:42 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2