Not sure what your issue is, but here's some pointers on your Switch statement and other code.
As far as I know, the use Switch
feature/module has been replaced in perl v5.10.1 with the given/when
feature, and is now deprecated. Personally, I have always found these statements a little odd and unreliable. And not irreplaceable by any means, as perl is such a flexible language.
In your case, I would recommend a hash lookup instead:
my %camp = ("ssss_uk_01B" => 1,
"ssss_uk_01C" => 2,
...);
my %cand = ("Brown" => 1,
"Cameron" => 2,
...);
$campaign_id = $camp{$campaign};
$candidate_id = $cand{$choice}
If you need to provide default values, you can use the defined-or
assignment operator:
$campaign_id //= "default value here";
Your substr
assignments are a bit wrong. First off, you use all four arguments, which in perldoc is described like this:
substr EXPR,OFFSET,LENGTH,REPLACEMENT
Which means that you replace your match with ''
the empty string. This affects the variable itself, and would delete your wanted data, if left on its own. Length in your case is not needed either, since it is the end of the string you are after. Lastly, you are saved by the fact that you use the return value of the substr
statement.
Finally, when I try out your assignments, I get a one-off error on your first assignment ($campaign
). The output is :ssss_uk_01
, and you expect ssss_uk_01
.
To use the substr
function correctly, you should use:
$campaign = substr $campaign, 9;
$validity = substr $validity, 9;
$choice = substr $choice, 7;