Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace binary form 0->1 and 1->0 value - perl

Tags:

perl

In my script i am dealing with binary value and i need to replace 0->1 and 1->0 at one place.

example : input digit = 10101001 output digit = 01010110

I tried $string =~ s/1/0/; and reverse function but that is getting fail to give me correct out put.

can some one help me out.

like image 955
Maverick Avatar asked Dec 03 '22 16:12

Maverick


1 Answers

Use tr:

my $str = '10101001';

$s =~ tr/01/10/;

print "$s\n";

Outputs:

01010110
like image 97
Miller Avatar answered Dec 24 '22 23:12

Miller