Be careful when comparing things for equality by stringifying: except when the values stringified conform to some rules to remove the danger (eg if each value has a fixed length), there is always the danger of false positives. For example, given:

join '', map { $_, $hash{$_} } ( qw/ page chap / );
you cannot tell the difference between these two:
{ page => 'chappage', chap => '' }, { page => '', chap => 'pagechap' },

There are two ways to avoid this problem: either you take advantage of some regularity of the data, or you get more complex. If the data has some regularity - if, say, it is not allowed to include a nul byte - you can use the unpermitted sequence as your delimiter:

join "\0", map { $_, $hash{$_} } ( qw/ page chap / );

If not - if the data can contain any arbitrary string - then you cannot avoid modifying the data at least enough to disambiguate the delimiter. So if you want to use 'x' as the delimiter, you need to encode 'x' in the data as something else. You can't encode it as 'xx', because that would be ambiguous (compared to an empty data element). So you might encode 'x' as 'xx', and use 'xyx' as the delimiter: now finally you've done enough to avoid ambiguity, and therefore to avoid false matches:

join 'xyx', map { s/x/xx/g; $_ } map { $_, $hash{$_} }, qw/ page cha +p /;

One final point: once you have solved the ambiguity problem, note that there is no point including invariant information in the stringified version. So when the keys are explicitly named, you might as well just encode the values:

join 'xyx', map { s/x/xx/g; $_ } map $hash{$_}, qw/ page chap /;

Hugo


In reply to Re^6: Is there better way to delete dups in an AoH? by hv
in thread Is there better way to delete dups in an AoH? by bradcathey

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.