Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reverse the order of two words in a string?

Tags:

perl

I have a string like this:

$a = "Mike , Tree "; 

I want to reverse it to "Tree, Mike".

Is there any function to do that?

like image 787
Tree Avatar asked Nov 23 '25 09:11

Tree


1 Answers

Split the string into two strings, flip them, and rejoin them.

Or, use a regex:

$a =~ s/(.+),(.+)/\2,\1/g;
like image 128
Andy Lester Avatar answered Nov 24 '25 23:11

Andy Lester