http://www.perlmonks.org?node_id=602113

qiau has asked for the wisdom of the Perl Monks concerning the following question:

Greetings fellows, I have a rather short question about "the fastest way to..". I have a string that kind of looks liks this:
$string = "123".\0. "1".\0.\0.\0. "23".\0.\0;
And so on, 4 chars for each value nullpadded. I have a procedure that fetches a value from the string based on a index but since I call that functions >60000 times I need the fastest way to get the value without the extra null-chars. Up to now I've tried two alternatives:
(undef, $value) = unpack("a$index A4", $string);
And:
$value = substr($string, $index, 4); $value =~ s/\0//g;
But neither regexps nor unpack feels like the fastest way to do it. Is there any other way?