Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Trouble with Transliterate Function

by choroba (Bishop)
on Aug 28, 2013 at 23:12 UTC ( #1051328=note: print w/replies, xml ) Need Help??


in reply to Trouble with Transliterate Function

Seems like an off by one error. I tried to fix it:
#!/usr/bin/perl use warnings; use strict; my $mask_input = '0000000000'; my $lastEnd = 0; while (<DATA>) { my (undef, $start, $end) = split '\s+'; my ($from, $length) = ($lastEnd, $start - $lastEnd); $length-- if $length > 0; substr($mask_input, $from, $length) =~ tr[\x00-\xff][N]; $lastEnd = $end; } substr($mask_input, $lastEnd) =~ tr[\x00-\xff][N]; print "# Desired: 000N00N00N\n"; print "# Obtained: $mask_input\n"; print $mask_input eq '000N00N00N' ? "OK" : "Wrong", "\n"; __DATA__ chrX 1 3 chrX 5 6 chrX 8 9

Updated: fixed the initial value of $lastEnd. Thanks abualiga.

لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Trouble with Transliterate Function
by abualiga (Scribe) on Aug 29, 2013 at 03:05 UTC

    Initially I ++voted your optimization but then noticed that if the first interval range does not start with 1, the first character will not be replaced with N. I think $lastEnd=0 is the correct initial value, as in BrowserUK's solution.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1051328]
help
Chatterbox?
NodeReaper puts the hammer down

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2017-12-17 04:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (462 votes). Check out past polls.

    Notices?