Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Swapping two parts of a string

by Harch84 (Acolyte)
on Jul 22, 2007 at 20:30 UTC ( [id://628142]=perlquestion: print w/replies, xml ) Need Help??

Harch84 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks I need your valued help again. I have a string which is passed to a perl variable from a html/javascript and returns a set of coordinates. An example of one of these coordinate strings would be this:

(55.950254, -3.187606)

Now what I need to do is to get rid of the comma in this string and then to take the first part of coordinate "55.950254" and put it in front of the other part "-3.187606". The output should look like this:

(-3.187606 55.950254)

I have managed to get rid of the comma using this command in Perl:

$origin =~ s/,//;
... but im not sure how I could swap the two strings around using perl? If it helps both parts should be exactly 9 characters long.
Thanks for your help

Replies are listed 'Best First'.
Re: Swapping two parts of a string
by FunkyMonk (Chancellor) on Jul 22, 2007 at 20:45 UTC
    my $input = "(55.950254, -3.187606)"; ( my $output = $input ) =~ s{([-\d.]+), # capture the first number to + $1 \s+ # at least one space ([-\d.]+) # capture the second to $2 } {$2 $1}x; # reverse $1 & $2 print $output;

    See perlre and perlretut for help with regular expressions, and Regexp Quote-Like Operators in perlop for s///.

    update: added references to documentation

Re: Swapping two parts of a string
by grep (Monsignor) on Jul 23, 2007 at 01:50 UTC
    FWIW a non regex answer
    my $input = "(55.950254, -3.187606)"; my $result = '('.join(' ',reverse(split(/[(),]/,$input))).')';
Re: Swapping two parts of a string
by GrandFather (Saint) on Jul 22, 2007 at 20:53 UTC

    Capture the two strings to be swapped using a regex then put them back together in opposite order:

    $origin =~ s/\( ([^ ,]+) \s*,\s* ([^ )]+) \) /($2 $1)/x;

    Note that the /x allows the spaces to be used to break up the expression for easier understanding.


    DWIM is Perl's answer to Gödel
Re: Swapping two parts of a string
by Harch84 (Acolyte) on Jul 22, 2007 at 22:37 UTC
    Hey thanks for the quick replies guys they have been a big help!
Re: Swapping two parts of a string
by robot_tourist (Hermit) on Jul 23, 2007 at 12:03 UTC

    I have nothing to add except: cool, the Royal Mile!

    How can you feel when you're made of steel? I am made of steel. I am the Robot Tourist.
    Robot Tourist, by Ten Benson

Re: Swapping two parts of a string
by oxone (Friar) on Jul 25, 2007 at 21:07 UTC
    Here's a much worse way to do this, which is neither big or clever (although it does work):
    my $input = "(55.950254, -3.187606)"; (my $result = join ' ', eval "reverse$input" ) =~ s/(.*)/($1)/;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://628142]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-18 02:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found