Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Can we make hash key check in 'if' condition case insensitive?

by choroba (Bishop)
on Feb 08, 2018 at 21:53 UTC ( #1208757=note: print w/replies, xml ) Need Help??


in reply to [SOLVED: Can we make hash key check in 'if' condition case insensitive?

To do a fast key lookup, the strings have to be equal, so the short answer is NO.

You can lowercase the keys of the hash, but you can store the original string as the value if you need it later:

#!/usr/bin/perl use warnings; use strict; my @castaways = qw( john Jane LUKE ); my @list_1 = qw( not John JANE luke ); my %hash = map { lc, $_ } @castaways; for my $person (@list_1) { if (exists $hash{ lc $person }) { print "$person is a castaway (as $hash{ lc $person }).\n"; } }

I used exists so even 0 can be a castaway.

($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: Can we make hash key check in 'if' condition case insensitive?
by ikegami (Pope) on Feb 08, 2018 at 23:35 UTC

    fc should be used for case-insensitive comparisons, not lc.

      fc should be used for case-insensitive comparisons, not lc

      Do you have a usage example for "fc" that actually works ?

      I certainly don't. On Win32 perls 5.24.0, 5.26.0, 5.27.8 and Ubuntu perl 5.26.0 (all of which provide documentation for the fc function), I tried the following script:
      use strict; use warnings; my $this = 'hello'; my $that = 'tata'; if(fc($this) eq fc($that)) {print "wtf\n"}
      In every case it simply output:
      Undefined subroutine &main::fc called at try.pl line 7.


      Update Aah .. you have to use feature 'fc'; to enable it.
      (Clearly stated at the very end of the documentation.)
      I personally would not recommend anything that requires feature to be enabled.

      Cheers,
      Rob

        I personally would not recommend anything that requires feature to be enabled.

        *shrug* You're free to use another implementation if you don't want to use perl's. Unicode::UCD might provide something.

      Just wanted to try this option out as well. So, I tried to use fc in the same example that choroba suggested and make it work. But it displays only "John" as a castaway.

      #!/localperl/bin/perl use warnings; use strict; use feature 'fc'; my @castaways = qw( john Jane LUKE ); my @list_1 = qw( not John JANE luke ); my %hash = map { $_, 1 } @castaways; for my $person (@list_1) { if ( $hash{ fc($person) }) { # Tried with exists as well. Same res +ult. print "$person is a castaway\n"; } }

      Result: John is a castaway

      So I think in my case, using lc is the only option. BTW I am using (v5.24.0)

        Change the map line to my %hash = map { fc $_, 1 } @castaways;, so that the hash keys are already folded, and I get:
        John is a castaway JANE is a castaway luke is a castaway

        You need fc here too

        my %hash = map { fc $_, 1 } @castaways;
        poj
Re^2: Can we make hash key check in 'if' condition case insensitive?
by Perl300 (Pilgrim) on Feb 08, 2018 at 22:14 UTC
    This is very useful to me and exactly what I was looking for. I would never have thought of creating hash with keys as lower case strings and values as original string. Thank you so much.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1208757]
help
Chatterbox?
and the grasshoppers chirp...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2018-02-25 06:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (312 votes). Check out past polls.

    Notices?