CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions: Re: [KevinR] How to change [1,2,3] to [1][2][3]: Edit Log



1arryb
User

Feb 26, 2009, 1:58 PM


Views: 10131
Re: [KevinR] How to change [1,2,3] to [1][2][3]

Kevin,

or, more simply:

Code
my $x = "[3,4,5]\n[ 3 , 4 , 5 ]\n";  
$x =~ s/,/][/g;
$x =~ tr/ //d;
print $x;

It takes a second scan to trim the white space (should that prove necessary, but it's sure easier to understand Laugh

UPDATE:

D'oh! We both forgot the '1, 2, 3' case:

Code
my @xs = ('[3,4,5]', '[ 3 , 4, 5 ]', '3 , 4 , 5');  
for my $x (@xs) {
$x =~ s/[\[\] ]//g; # normalize all variants to '3,4,5'.
$x =~ s/,/][/g;
print "[$x]\n";
}


Cheers,

Larry


(This post was edited by 1arryb on Feb 26, 2009, 2:09 PM)


Edit Log:
Post edited by 1arryb (User) on Feb 26, 2009, 2:09 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives