Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 161
  • Last Modified:

replace

how to replace all comma's in a variable with an other character ?

tnx in advance
0
oostwijk
Asked:
oostwijk
  • 2
  • 2
1 Solution
 
ahoffmannCommented:
$var=~s/,/x/g;
0
 
interiotCommented:
$var =~ tr/,/x/;


Using tr/// is a little over 4 times as fast as using s///;
0
 
interiotCommented:
You can see that by running the following program:


#!/usr/bin/env perl
$^W=1;

use Benchmark;

my $str = "asdfklj,123ja09d,21349a,asd,f,23,,4,,,123a09sdfjl2345,asdj034,";

timethese(500000,
{       'S' => sub {
                my $s = $str;
                $s =~ s/,/x/g;
        },
        'TR' => sub {
                my $s = $str;
                $s =~ tr/,/x/;
        }
});
0
 
ahoffmannCommented:
nice Benchmark ;-)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now