Updated: Re question from OPuse strict; use warnings; my $val1 = 5234; my @digits = split(//,$val1); my $string = join ('_',@digits); $string =~ s/_(\d)$/$1/ if @digits == 4; print $string, "\n"; #prints 5_2_34 # #$val1 5234 prints 5_2_34 #$val1 52 prints 5_2 #$val1 523 prints 5_2_3
I am just trying to demonstrate a technique. Once you see how this works, you can adapt it for your exact specific needs. Like maybe if @digits is >4, that is an error or whatever. A subroutine would be like this (shamelessly plugging my code into toolic's loop structure, thanks toolic!): (this is 2nd update)
for (qw(5234 523 52)) { print append($_ , '_'), "\n"; } sub append { my ($val, $character) = @_; my @digits = split(//,$val); my $string = join ($character,@digits); $string =~ s/$character(\d)$/$1/ if @digits >= 4; return $string; }
In reply to Re: Check substitute digit
by Marshall
in thread Check substitute digit
by tart
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |