tr/ last char x$

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
 
A

anno4000

ash said:
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$/;

Right, that's how tr/// works. It always changes the whole string.
Read the documentation. Use substr() to restrict it to the last
character.

my @ts_number = qw( 0109 7402 5586);
my @ts_roundup;
substr( $_, -1, 1) =~ tr/0123456789abcde/123456789abcdef/ for
@ts_roundup = @ts_number;
print "$ts_number[ $_] -> $ts_roundup[ $_]\n" for 0 .. $#ts_roundup;

Anno
 
A

ash

Thanks a million guys!

Both solutions are brilliant, yep that is what i was trying to do have
a neat way to just incrementing hex numbers
 
A

anno4000

Glenn Jackman said:
At said:
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

Are you just incrementing a list of hex numbers ?

my @ts_roundup = map {sprintf '%04x', 1+hex} @ts_number;

Yes, but that's different. None of the solutions so far did a carry.
No idea if that's intended that way or if it's an oversight.

Anno
 
T

Tad McClellan

ash said:
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)


Because tr/// does not use regular expressions!

$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$/;


Dollar sign is a regex metacharacter, but since tr does not use
regular expressions, a dollar sign is just a dollar sign.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,114
Latest member
GlucoPremiumReview
Top