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.

  1. The potentially ambiguous "Enter whole or partial name for key (Enter to exit)" is now "Enter whole or partial name for key (just hit Return to exit).".
  2. Where a match only differs in case, it is shown as NO_CASE instead of PARTIAL; for example, Cola matches cola if case is ignored:
    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).

— 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

    I tinkered some more:

    There was a bug which I've fixed. I've no idea why, but I'd coded for only one NO_CASE. Obviously, you can have ABC, Abc, abc, AbC, and so on.

    I decided that I was doing too much work with @matches. I changed that to %matches and, instead of removing elements from the array by recreating it using grep, I'm simply using delete to remove key/value pairs. This resulted in much cleaner code and, I imagine, would be a lot more efficient.

    There was also an interesting point regarding the contents of the "Value" column produced by printf. Having introduced undef values in my test data for basic edge-case checks, this meant that there was either a price (number) or '<undefined>' (string). A %s worked fine for both when the price was 1.25; however, a price of 3.00 was converted to just 3.

    My introduction of undef may have been completely artificial and would never occur in the OP's data: in this case, a %.2f would probably suffice throughout. I've left the undef values as it was an interesting exercise dealing with this. I can see potential improvements here but won't go into that further unless it is something the OP really wants.

    As before, I'll put the new code and another sample run in a spoiler.

    — Ken

      my $fmt = "%-7s %-@{[max map length, keys %$hash]}s ";
      Definitely adding this to my snippet file! But I've never seen this syntax before. What is triggering the code evaluation of max(...) here?
        > What is triggering the code evaluation of max(...) here?

        "  @{[...]}  " is an idiomatic pseudo-operator by combining several mechanisms

        • String interpolation work not only for arrays but also for dereferencing of arrays.

          " @{$a_ref} "

        • The [...] marks an anonymous array-ref.

          " @{[ 1, 2, 3 ]} "

        • Lists can also include code statements to be executed

          " @{[ 1, 2, some_code() ]} "

        • The returned list will be included into the string.

          DB<6> p ">>> @{[ 1, 2, time() ]} <<<" >>> 1 2 1680647910 <<< DB<7>

        Cheers Rolf
        (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
        Wikisyntax for the Monastery

        update

        added code examples for each step

        G'day ibm1620,

        The @{[...]} construct is one of many that are referred to as "Perl secret operators and constants".

        This one, called "Baby cart", has been around for a very long time. Follow that link for an answer to your question as well as other information.

        I have been using it since the 1990s and, as I recall, wasn't even aware that it was a perlsecret until much later. I think I first encountered it in one of the "O'Reilly Perl books". I don't remember which one; although, "Advanced Perl Programming" is a likely candidate. Note that's the original edition; "Advanced Perl Programming, 2nd edition" is completely different — a very poor choice of title in my opinion.

        — Ken