Multiple if-else error
I have a problem with the if-else statement. Even if the $key
one I am using matches the text in my expression if
, the operator else
I have at the bottom is also evaluated, so I get multiple values ββfor each $key
, which is the problem. What am I doing wrong here?
my $key = $line[0];
if ($key eq 'this_setting') {
my $counter1 = $counter + 1;
my $counter2 = $counter + 2;
$value = join(' ', @line[$counter1..$counter2]);
$my_setting_hash{$key} = $value;
}
if ($key eq 'some_setting_abc') {
my $counter1 = $counter + 1;
my $counter2 = $counter + 2;
$value = join(' ', @line[$counter1..$counter2]);
$my_setting_hash{$key} = $value;
}
if ($key eq 'another_setting_123') {
my $counter1 = $counter + 1;
my $counter3 = $counter + 3;
$value = join(' ', @line[$counter1..$counter3]);
$my_setting_hash{$key} = $value;
}
else {
my $counter1 = $counter + 1;
$value = $line[$counter1];
$my_setting_hash{$key} = $value;
}
Why is else
n't this operator bypassed if one of my operators is being evaluated if
?
source to share
You need to link them together with elsif
:
my $key = $line[0];
if ($key eq 'this_setting') {
my $counter1 = $counter + 1;
my $counter2 = $counter + 2;
$value = join(' ', @line[$counter1..$counter2]);
$my_setting_hash{$key} = $value;
}
elsif ($key eq 'some_setting_abc') {
my $counter1 = $counter + 1;
my $counter2 = $counter + 2;
$value = join(' ', @line[$counter1..$counter2]);
$my_setting_hash{$key} = $value;
}
elsif ($key eq 'another_setting_123') {
my $counter1 = $counter + 1;
my $counter3 = $counter + 3;
$value = join(' ', @line[$counter1..$counter3]);
$my_setting_hash{$key} = $value;
}
else {
my $counter1 = $counter + 1;
$value = $line[$counter1];
$my_setting_hash{$key} = $value;
}
Otherwise, the first two operators if
do not depend on the third operator if
/ else
.
source to share
As already pointed out, you need the elsif keyword
However, another solution is to put your special rules for each key in a hash so that you can share the code:
my %key_length = (
this_setting => 1,
some_setting_abc => 1,
another_setting_123 => 2,
);
my $key = $line[0];
my $index_low = $counter + 1;
my $index_high = $index_low + ($key_length{$key} // 0);
$my_setting_hash{$key} = join ' ', @line[ $index_low .. $index_high ];
source to share
Let's say the $key
value 'some_setting_abc'
. Your first one if
doesn't apply, but the second one if
does. The third one if
does not apply, but that one does else
, so it is executed. As @TedHopp pointed out , you will need one if
with chained elsif
and final one else
.
However, I want to point out that there is a lot of duplication in your code. Life is easier when you write your code a little more succinctly:
my $key = $line[0];
my $index = $counter + 1;
if (($key eq 'this_setting') or ($key eq 'some_setting_abc')) {
$my_setting_hash{$key} = join ' ', @line[$index .. ($index + 1)];
}
elsif ($key eq 'another_setting_123') {
$my_setting_hash{$key} = join ' ', @line[$index .. ($index + 2)];
}
else {
$my_setting_hash{$key} = $line[$index];
}
source to share