Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by Perl300 (Pilgrim)
on Feb 09, 2018 at 17:17 UTC ( #1208843=note: print w/replies, xml ) Need Help??


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

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)

  • Comment on Re^3: Can we make hash key check in 'if' condition case insensitive?
  • Download Code

Replies are listed 'Best First'.
Re^4: Can we make hash key check in 'if' condition case insensitive?
by pryrt (Priest) on Feb 09, 2018 at 17:26 UTC
    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

      Thank you ikegami, pryrt and poj. I learned something new again.

Re^4: Can we make hash key check in 'if' condition case insensitive?
by poj (Monsignor) on Feb 09, 2018 at 17:23 UTC

    You need fc here too

    my %hash = map { fc $_, 1 } @castaways;
    poj

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1208843]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2018-02-21 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (282 votes). Check out past polls.

    Notices?