A
ash
I am trying to substitute the last char in each array of strings,
could some please explain why the tr/ function is not replacing just
the last char (see below)
The following works but is very bulky
$ts_roundup[$x] = $ts_number[$x];
if ($ts_number[$x]=~ /0$/)
{
$ts_roundup[$x]=~ s/0$/1/;
}elsif ($ts_number[$x]=~ /1$/)
{
$ts_roundup[$x]=~ s/1$/2/;
} elsif ($ts_number[$x]=~ /2$/)
{
$ts_roundup[$x]=~ s/2$/3/;
} etc etc
Correct Result:
ts number = 0109 ts round up =010a
ts number = 7402 ts round up =7403
ts number = 5586 ts round up =5587
I tried the following to reduce it to a one liner, This translates all
the characters in the string, not just the last character
$ts_roundup[$x]=~ tr/0$1$2$3$4$5$6$7$8$9$a$b$c$d$e$/1$2$3$4$5$6$7$8$9$a
$b$c$d$e$f$/;
Result:
ts number = 0109 ts round up =121a
ts number = 7402 ts round up =8513
ts number = 5586 ts round up =6697
The following line of code does not produce the correct answer either
$ts_roundup[$x]=~ tr/0$1$2$3$4$5$6$7$8$9$a$b$c$d$e$/123456789abcdef/;
Result:
ts number = 0109 ts round up =131f
ts number = 73ee ts round up =f7ff
ts number = 9642 ts round up =fd95
could some please explain why the tr/ function is not replacing just
the last char (see below)
The following works but is very bulky
$ts_roundup[$x] = $ts_number[$x];
if ($ts_number[$x]=~ /0$/)
{
$ts_roundup[$x]=~ s/0$/1/;
}elsif ($ts_number[$x]=~ /1$/)
{
$ts_roundup[$x]=~ s/1$/2/;
} elsif ($ts_number[$x]=~ /2$/)
{
$ts_roundup[$x]=~ s/2$/3/;
} etc etc
Correct Result:
ts number = 0109 ts round up =010a
ts number = 7402 ts round up =7403
ts number = 5586 ts round up =5587
I tried the following to reduce it to a one liner, This translates all
the characters in the string, not just the last character
$ts_roundup[$x]=~ tr/0$1$2$3$4$5$6$7$8$9$a$b$c$d$e$/1$2$3$4$5$6$7$8$9$a
$b$c$d$e$f$/;
Result:
ts number = 0109 ts round up =121a
ts number = 7402 ts round up =8513
ts number = 5586 ts round up =6697
The following line of code does not produce the correct answer either
$ts_roundup[$x]=~ tr/0$1$2$3$4$5$6$7$8$9$a$b$c$d$e$/123456789abcdef/;
Result:
ts number = 0109 ts round up =131f
ts number = 73ee ts round up =f7ff
ts number = 9642 ts round up =fd95