Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have strings like start range and end range and i have to check whether the given value falls in the given range. For example,

$startrange = 'AC23D'; $endrange = 'D25ET'; $givenvalue = 'C3RK5';

I tried to generate all the values between start range and end range using for loop, but i am getting alphabetic range or numeric range, For example,

for (aa..zz) or for ( 1..1000) or for (a2..g5) #working fine for (A12Z..B23Z) #not working if number comes in between alphabets
If i have both alphabets and numbers in the range, i could not able to generate the range properly.

How can i check whether the given value falls in the given range. (maximum characters is 5 only as in the example).

Replies are listed 'Best First'.
Re: To check whether value falls in the given range
by quester (Vicar) on Nov 27, 2006 at 08:08 UTC
    The usual way of testing if a number is within a certain range,
    if ($startrange <= $givenvalue and $givenvalue <= $endrange) ...
    
    can be extended to strings, provided that the range is supposed to be in the usual ASCII collating sequence,
    if ($startrange le $givenvalue and $givenvalue le $endrange) ...
    
    This is about three times faster than sorting the three values.
Re: To check whether value falls in the given range
by cLive ;-) (Prior) on Nov 27, 2006 at 07:49 UTC
    If they're all of the same format:
    my $startrange = 'AC23D'; my $endrange = 'D25ET'; my $givenvalue = 'C3RK5'; if ( (sort $startrange,$givenvalue,$endrange)[1] eq $givenvalue ) { print "This is OK\n"; }
Re: To check whether value falls in the given range
by jwkrahn (Abbot) on Nov 27, 2006 at 07:55 UTC
    You probably want something like this:
    my $startrange = 'AC23D'; my $endrange = 'D25ET'; my $givenvalue = 'C3RK5'; if ( $givenvalue ge $startrange and $givenvalue le $endrange ) { # do something }