Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Reverse String

by bimleshsharma (Beadle)
on Jul 21, 2011 at 06:08 UTC ( #915795=perlquestion: print w/ replies, xml ) Need Help??
bimleshsharma has asked for the wisdom of the Perl Monks concerning the following question:

How do we reverse string

Comment on Reverse String
Re: Reverse String
by bimleshsharma (Beadle) on Jul 21, 2011 at 06:11 UTC

    Try this

    $line = 'Bimlesh Sharma';
    $line =~ s/(\w+)/ scalar (reverse($1)) /eg;
    print $line;
    # prints "hselmiB amrahS"

      I understand what you're doing here; reversing individual "words" (by Perl's definition of what matches against \w), while preserving their original position within the string. If you were actually reversing the string (as your original post implies), you would just use reverse $string, taking context into consideration.

      Going back to your solution for a moment: \w matches alpha characters, plus 0-9 and underscore, but not hyphen or apostrophe. So "What's up" becomes "wahW's pu". That may be what you're after, or maybe you would prefer to count apostrophe as a character, so that the string becomes "s'wahW pu". While you're at it you might want to count hyphen (or again, maybe not), so that "hyper-drive" becomes evird-repyh" instead of what happens with your solution, which is 'repyh-evird'. But I'm really not sure what you're after as neither your solution nor my suggestions actually match the most simple and obvious solution to "Reverse string".

      But I do wonder, in posting a question, and immediately following it up with your own answer, if this may be more appropriate in Meditations (where people post interesting discoveries, thoughts, ...meditations), or Cool Uses For Perl (where people post snippets of code that show Perl doing interesting things). I would say Categorized Questions and Answers, except that has been a pretty slow category for a few years here.


      Dave

Re: Reverse String
by moritz (Cardinal) on Jul 21, 2011 at 06:30 UTC
Re: Reverse String
by Anonymous Monk on Jul 21, 2011 at 06:46 UTC
Re: Reverse String
by sundialsvc4 (Abbot) on Jul 21, 2011 at 14:58 UTC

    If you want to reverse the order of the individual words, or perhaps if you specifically don’t want to do that, then the split function can come in very handy.   This would take the individual words and create a list, one entry for each word, which you can now process and reorder using functions like reverse, shift, push.

    It is very, very important that you test this code thoroughly with all kinds of real-world inputs.   split is powerful, but it is no panacea.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://915795]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2014-10-01 17:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (31 votes), past polls