#!/usr/bin/perl use strict; use warnings; my $line = '~1~,,~234~,7,~,,~,8'; my $newline = ""; # löl my $in = 0; for my $i ( 0 .. length($line) ) { my $c = substr($line, $i, 1); $newline .= $c; print $newline; # determine whenever we are in a field or outside. if( $in == 0 and $c eq '~' ) { # now we're in a field $in = 1; next; }if( $in == 1 and $c eq '~' ) { # now we're going outside to play hide and ... $in = 0; next; }if( $in == 0 and $c eq ',' ) { # we're outside, now check if next char is a , too if( substr($line, $i, 2) eq ',,' ) { print ' -> ' . substr($line, $i, 2); $newline .= '~~'; } } print "\n"; } print( $newline );