in reply to How to detect non printable characters and non white space characters? [RESOLVED]

It seems you got De Morgan wrong: it should be
# If not white space character AND not non printable character remove +element

That's probably because there are too many negations. If you have an if with else , using negation in the condition makes it really hard to understand. So, instead of

if ($$hoh_ref{$key}{$value} !~ /[^[:print:]]/g && $$hoh_ref{$key}{$value} !~ /\s/) { delete $$hoh_ref{$key}{$value}; } elsif ($$hoh_ref{$key}{$value} =~ /[^[:print:]]/g) { while ($$hoh_ref{$key}{$value} =~ /[^[:print:]]/g) { print "Non Printable Characater:\t$&\n"; } }

you can use a bit simpler

if ($$hoh_ref{$key}{$value} =~ /[^[:print:]]/) { # no /g neede +d while ($$hoh_ref{$key}{$value} =~ /[^[:print:]]/g) { print "Non Printable Characater:\t$&\n"; } } elsif ($$hoh_ref{$key}{$value} !~ /\s/) { delete $$hoh_ref{$key}{$value}; }

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: How to detect non printable characters and non white space characters?
by thanos1983 (Parson) on Feb 17, 2017 at 11:03 UTC

    Hello choroba,

    I thought about it when I was implementing the solution to use and instead of or but it just did not make sense to me and it did not work when I was testing.

    You are absolutely right simpler the better. Thank you for your time and effort reading and replying to my question.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      I likes the post from choroba++. A few minor suggestions:
      • I show a different indenting style below, but that is no big deal to me.
      • More important to me is: $href->{key1}{key2}I think that reads easier than use of $$href{key1}{key2}. My preference is to only use $$scalar_ref to de-reference a scalar value.
      • I replaced $& with a simple $1 capture. There can be some performance issues with $& with Perl < 5.20. See perlre. My preference is not to use $& unless needed, and here it is not.
      #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $str = 'a[bdy]dfjaPÑsdafÜ'; my $str_2 = 'WAP'; my $hoh_ref = { hash_1 => { a => 'a[bdy]dfjaPÑsdafÜ', b => 'WAP' }, hash_2 => { c => 'Te st' } }; print "Original Hash:\n"; print Dumper $hoh_ref; foreach my $main_key (sort keys %{$hoh_ref}) { foreach my $sub_key (keys %{$hoh_ref->{$main_key}}) { if ($hoh_ref->{$main_key}{$sub_key} =~ /[^[:print:]]/) { while ($hoh_ref->{$main_key}{$sub_key} =~ /([^[:print:]])/g) { print "Non Printable Character:\t$1\n"; } } elsif ($hoh_ref->{$main_key}{$sub_key} !~ /\s/) { delete $hoh_ref->{$main_key}{$sub_key}; } } } print "\nResult Hash:\n"; print Dumper $hoh_ref; __END__ Original Hash: $VAR1 = { 'hash_1' => { 'a' => 'a[bdy]dfjaPÑsdafÜ', 'b' => 'WAP' }, 'hash_2' => { 'c' => 'Te st' } }; Non Printable Character: à Non Printable Character: ‘ Non Printable Character: à Non Printable Character: œ Result Hash: $VAR1 = { 'hash_1' => { 'a' => 'a[bdy]dfjaPÑsdafÜ' }, 'hash_2' => { 'c' => 'Te st' } };
      Update: re-indented the code above. I think the result is better now. My editor barfed when converting the OP's tabs to spaces.