in reply to Re: Check if key exist in hash but not exact match
in thread Check if key exist in hash but not exact match
I tinkered with my previous code (Re: Check if key exist in hash but not exact match) and made a couple of improvements.
Name: Cola Match Key Value ----- --- ----- NO_CASE cola 1.25 PARTIAL Coca Cola 1.25 PARTIAL Pepsi Cola 1.25 PARTIAL Undead Cola <undefined>
As there are only a few code changes, and the sample run is the same as before except for the Name: Cola (shown above), I've put all of this in a spoiler (view at your leisure).
New code:
#!/usr/bin/env perl use v5.36; use List::Util qw{first max}; my %price = ( 'Coca Cola' => 1.25, coke => 1.25, cola => 1.25, 'Pepsi Cola' => 1.25, pizza => 12.00, sandwich => 3.00, 'Undead Cola' => undef, ); say 'Enter whole or partial name for key (just hit Return to exit).'; while (1) { print "\nName: "; my $name = <STDIN>; chomp $name; unless (length $name) { say 'Exiting ...'; last; } unless ($name =~ /^[A-Za-z0-9 ]+$/) { say 'Only alphanumeric+space name searches allowed.'; next; } check_keys(\%price, $name); } sub check_keys ($hash, $name) { my $fmt = "%-7s %-@{[max map length, keys %$hash]}s %s\n"; my @matches = grep /$name/i, keys %$hash; if (@matches) { printf $fmt, qw{Match Key Value}; printf $fmt, qw{----- --- -----}; if (exists $hash->{$name}) { @matches = grep !/^$name$/, @matches; printf $fmt, 'EXACT', $name, $hash->{$name} // '<undefined +>'; } if (@matches) { my $no_case = first {/^$name$/i} @matches; if ($no_case) { @matches = grep !/^$no_case$/, @matches; printf $fmt, 'NO_CASE', $no_case, $hash->{$no_case} // '<undefi +ned>'; } if (@matches) { for my $match (sort @matches) { printf $fmt, 'PARTIAL', $match, $hash->{$match} // '<undefi +ned>'; } } } } else { say "No keys match '$name'."; } return; }
New sample run (same inputs as previously used):
Enter whole or partial name for key (just hit Return to exit). Name: cok Match Key Value ----- --- ----- PARTIAL coke 1.25 Name: col Match Key Value ----- --- ----- PARTIAL Coca Cola 1.25 PARTIAL Pepsi Cola 1.25 PARTIAL Undead Cola <undefined> PARTIAL cola 1.25 Name: Coca Cola Match Key Value ----- --- ----- EXACT Coca Cola 1.25 Name: Undead Cola Match Key Value ----- --- ----- EXACT Undead Cola <undefined> Name: Cola Match Key Value ----- --- ----- NO_CASE cola 1.25 PARTIAL Coca Cola 1.25 PARTIAL Pepsi Cola 1.25 PARTIAL Undead Cola <undefined> Name: cola Match Key Value ----- --- ----- EXACT cola 1.25 PARTIAL Coca Cola 1.25 PARTIAL Pepsi Cola 1.25 PARTIAL Undead Cola <undefined> Name: ^cola$ Only alphanumeric+space name searches allowed. Name: (?{/path/to/evil_code}) Only alphanumeric+space name searches allowed. Name: Nightingale Tongues in Aspic No keys match 'Nightingale Tongues in Aspic'. Name: Exiting ...
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Check if key exist in hash but not exact match
by kcott (Archbishop) on Apr 04, 2023 at 19:21 UTC | |
by ibm1620 (Hermit) on Apr 04, 2023 at 21:36 UTC | |
by LanX (Saint) on Apr 04, 2023 at 21:51 UTC | |
by eyepopslikeamosquito (Archbishop) on Apr 04, 2023 at 22:23 UTC | |
by kcott (Archbishop) on Apr 04, 2023 at 22:41 UTC | |
by ibm1620 (Hermit) on Apr 05, 2023 at 12:11 UTC |