My team was debating the idea of writing a large file using pipe characters to delimit the fields, and I pointed out that our data includes pipe characters. So the tilde was suggested, but I found some of those too.

So the obvious question arises, can I find a printable ASCII character that never appears in our big file?

I'd like to do this with just a single pass through the file. For locating a pipe character, all I had to say was

while (<>) {last if /\|/} printf "| seen at index %d in %s\n", index($_, '|'), $_;

Update: Should have mentioned that I'm going to run any executable solutions against a 2 GB file. So efficiency may be an issue.

Later: Thanks for the insights! I applaud almut's choice of an indicator array @seen, updated with $seen[ord $chr]++, which should be more efficient than the hash version $seen{$chr}++.

I think I will try kennethk's solution using \Q, both as written and then with the indicator array, as soon as my daughters are in bed and I can connect to my server again.

(Just a minute, sweetie, Daddy will be right with you ...)

It's true the indicator array doesn't offer a builtin keys function to list the seen items, but a grep should do just fine.


In reply to Find what characters never appear by Narveson

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.