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. |