How does a pearl recognize both lower and upper input?

I'm currently trying to figure out how I can get my perl script to accept both a lowercase and a uppercase version of a letter.

Example.

my %channels = (a => 'test', b => 'test2', c => 'test3', d => 'test4', e => 'test5', f => 'test6', g => 'test7');

So when I want to enter a test, I can either do or A, and he will take it

To calculate the problem:

When running the script, I had a problem when you needed to enter a if you need a test. This is all good for me, but other people wanted capital A to be made instead. I am trying to give them the opportunity to do so.

Thank you all

+4
source share
5 answers

For your hash keys ( one alphabet as a key ) try a logically defined operator.

//= > . , , .

print $channels{$input} // $channels{lc($input)};
+2

print $channels{lc($input)};

, . .

+2

, , , , , .. $hash{foo}, $hash{foo} .. .

FETCH, STORE DELETE . , , Hash:: Case.

, ( , ..) .

+1
source

I would keep all the hash keys in lower case and convert the input keys to lower case:

for my $inputKey ('a', 'A', 'b', 'B') {
    print $channels{lc($inputKey)}, "\n";
}
+1
source

With regexit is not shorter, but another way to solve it:

my %hash = {a => 'test', b => 'test2'};
my $read_input;

foreach my $key (keys %hash){
  if($key =~ /${read_input}/i){ #note the i for 'ignore case'
    print $hash{$key}; 
  }
}

You basically iterate over the keys of your hash and compare them with the input. If you find the correct option, you can print the value.

But nonetheless: you can simply convert each input to lowercase and then access the hash.

0
source

Source: https://habr.com/ru/post/1651741/


All Articles