hawtin has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Various ways to concatenating an array of strings
by GrandFather (Saint) on Mar 30, 2006 at 18:59 UTC | |
by hawtin (Prior) on Mar 30, 2006 at 19:49 UTC | |
by Tanktalus (Canon) on Mar 30, 2006 at 20:18 UTC | |
by ikegami (Patriarch) on Mar 30, 2006 at 19:59 UTC | |
by hawtin (Prior) on Mar 30, 2006 at 20:03 UTC | |
|
Re: Various ways to concatenating an array of strings
by swampyankee (Parson) on Mar 30, 2006 at 18:59 UTC | |
|
Re: Various ways to concatenating an array of strings
by whio (Beadle) on Mar 30, 2006 at 21:11 UTC | |
by hawtin (Prior) on Mar 31, 2006 at 12:10 UTC | |
|
Re: Various ways to concatenating an array of strings
by duff (Parson) on Mar 30, 2006 at 19:09 UTC |