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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.