Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by Perl300 (Pilgrim)
on Feb 08, 2018 at 21:47 UTC ( #1208755=perlquestion: print w/replies, xml ) Need Help??
Perl300 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all.

I am using this same approach from "Intermediate Perl" to check if an element of two dimensional array exist in a hash:

my %hash = map { $_, 1 } @castaways; foreach my $person (@list_1) { if( $hash{$person} ) { print "$person is a castaway.\n"; #do something more } }
My problem is, the strings I'll have in @castaways and @list_1 are uppercase as well as lowercase. So as of now, the if condition fails if the string in @castaways is 'Hello' while in @list_1 it is 'hello'.

Is there any way to make the if condition do case insensitive match? I am trying to avoid making any change in the @castaways and @list_1 as they are taken from different sources and will be sent to other applications for processing. I don't know if changing case would mess things up for others.

UPDATE: Marked as solved in subject.

  • Comment on [SOLVED: Can we make hash key check in 'if' condition case insensitive?
  • Download Code

Replies are listed 'Best First'.
Re: Can we make hash key check in 'if' condition case insensitive?
by choroba (Bishop) on Feb 08, 2018 at 21:53 UTC
    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,

      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

        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)

      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.
Re: Can we make hash key check in 'if' condition case insensitive?
by Anonymous Monk on Feb 08, 2018 at 21:58 UTC
Re: [SOLVED: Can we make hash key check in 'if' condition case insensitive?
by afoken (Abbot) on Feb 10, 2018 at 18:45 UTC

    Tie::CPHash

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1208755]
Approved by karlgoethebier
help
Chatterbox?
and dust plays in a shaft of sunlight...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2018-05-23 18:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?