Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by Perl300 (Friar)
on Feb 09, 2018 at 17:17 UTC ( [id://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 (Abbot) 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 (Abbot) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1208843]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-19 18:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found