I have an array of strings (some of which may possibly contain characters in the range \x7f-\xff. I need to concatenate all the strings together at some point. Originally I had:
$combine = join('',@ret);
However a for some reason (lost in the mists of time, I suspect it was something to do with efficiency for a few important cases) I changed all the concatenations to:
$combine = pack("a*" x ($#ret + 1), @ret);
Now that worked for a considerable time until I recently discovered an unexpected behaviour. This can be illustrated by:
foreach my $item (@ret) { if($item =~ /([\x7f-\xff])/) { print "Array ...".substr($`,-5)." ||".$1. "|| ".substr($',0,5)."\n"; } } my $via_join = join('',@ret); if($via_join =~ /([\x7f-\xff])/) { print "Join ...".substr($`,-5)." ||".$1. "|| ".substr($',0,5)."\n"; } my $via_pack = pack("a*" x ($#ret+1),@ret); if($via_pack =~ /([\x7f-\xff])/) { print "Pack ...".substr($`,-5)." ||".$1. "|| ".substr($',0,5)."\n"; }
Which prints out:
Array ...s Neg ||≤|| cios
Join ...s Neg ||≤|| cios
Pack ...s Neg ||├|| │cios
Why does the pack modify the character? Is there a better way to concatenate or will I have to discover why I rejected join() all those months ago?
This is running under 5.8.6 on a Windows XP machine. The text strings orginated in an XML file (ie using ó) and are meant to be "utf-8".
Thanks for any help
In reply to Various ways to concatenating an array of strings by hawtin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |