-2

Based on the user input i.e. $release_num, I have to take a call on a particular action.

Its working in certain cases but fails in some other cases. Need help in refining the final solution.

Code:

my $release_num;

my $short_release_num = substr($release_num, 0, 4);

if (($short_release_num eq '20a0') or ($short_release_num eq '20a1') or ($short_release_num eq '20a2'))
{
    print " Build is required \n";
}
else
{
    print " Build is NOT required \n";
}

Probable values of user input $release_num are as follows:

$release_num = "20a1";
$release_num = "20a1a11";
$release_num = "20a10";
$release_num = "20a10a11";
$release_num = "20a11";
$release_num = "20a11a1";

While my code works for some of the specific cases (20a1), but its not a generic solution as it fails for cases like 20a10a11. Please suggest how the code can be improved.

For Example: If the input is 20a10 or 20a10a11, the output should be "Build is NOT required" whereas I'm getting the output as "Build is required".

Expected Result:

For following inputs we should get "Build is required":

$release_num = "20a0";
$release_num = "20a0a1";
$release_num = "20a1";
$release_num = "20a1a2";
$release_num = "20a2a11";
$release_num = "20a2a1";

For following inputs we should get "Build is NOT required":

$release_num = "20a10";
$release_num = "20a10a11";
$release_num = "20a11";
$release_num = "20a11a1";
Yash
  • 2,944
  • 7
  • 25
  • 43
  • Looks like the solution is in the title. – Biffen Mar 07 '18 at 16:31
  • @toolic: yes, thats the issue. For an input like 20a10, the output should be "Build is NOT required" which is not happening. – Yash Mar 07 '18 at 16:41
  • 1
    You listed 6 example inputs, but you didn't say which of the 6 values should match and which shouldn't. Please explain what strings should match and which shouldn't! – ikegami Mar 07 '18 at 16:44

2 Answers2

1

Based on the if cases given, I'm guessing the desired answer is more like

if ( $release_num =~ /^20a[012](?!\d)/ ) {
    print " Build is required \n";
}
else {
    print " Build is NOT required \n";
}
Ed Grimm
  • 548
  • 5
  • 13
0

Example inputs are good, better would be if you could additionally explain the rules for when a "build is required" and when it isn't. Based on your (possibly incomplete!) example inputs:

use warnings;
use strict;
use Test::More;

my $build_is_required_regex = qr/^20a\d(?!\d)/;

like   "20a0",     $build_is_required_regex;
like   "20a0a1",   $build_is_required_regex;
like   "20a1",     $build_is_required_regex;
like   "20a1a2",   $build_is_required_regex;
like   "20a2a11",  $build_is_required_regex;
like   "20a2a1",   $build_is_required_regex;
unlike "20a10",    $build_is_required_regex;
unlike "20a10a11", $build_is_required_regex;
unlike "20a11",    $build_is_required_regex;
unlike "20a11a1",  $build_is_required_regex;

done_testing;

So that means:

if ( $release_num =~ /^20a\d(?!\d)/ ) {
    print " Build is required \n";
}
else {
    print " Build is NOT required \n";
}
haukex
  • 2,973
  • 9
  • 21