First, /g means "global", which means search all the matching results, don't stop at the first one (either one result at a time, or all of them at once). This technically doesn't change anything, but you just need to find that there is one non-printable character to keep the string. So the first regex could just be /[^[:print:]]/.

The straightforward solution to your problem is to use an and rather than an or in your condition. You want a string that (only contains valid printable chars) AND (only contains valid non-space chars) => (does not contain non-printables) AND (does not contain spaces). De Morgan's laws might also be a good read on how to negate a condition with ORs and ANDs.

However, I think you can obtain something easier to read with unless (which is an "if not") and next which will stop processing the current element in a loop, and go on to the next.

# Delete the value and try the next one unless it has a non-printable, + or a space delete $$hoh_ref{$key}{$value} and next unless /[^[:print:]]/ or /\s/; while ($$hoh_ref{$key}{$value} =~ /([^[:print:]])/g) { print "Unprintable !\n"; }


In reply to Re: How to detect non printable characters and non white space characters? by Eily
in thread How to detect non printable characters and non white space characters? [RESOLVED] by thanos1983

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.