Task: Range expansion



C#: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {
        var rangeString = "-6,-3--1,3-5,7-11,14,15,17-20";
        var matches = Regex.Matches(rangeString, @"(?<f>-?\d+)-(?
-?\d+)|(-?\d+)");
        var values = new List<string>();

        foreach (var m in matches.OfType<Match>())
        {
            if (m.Groups[1].Success)
            {
                values.Add(m.Value);
                continue;
            }

            var start = Convert.ToInt32(m.Groups["f"].Value);
            var end = Convert.ToInt32(m.Groups["s"].Value) + 1;

            values.AddRange(Enumerable.Range(start, end - start).Select(v => v.ToString()));
        }

        Console.WriteLine(string.Join(", ", values));
    }
}








Fortran:       MODULE HOMEONTHERANGE
       CONTAINS	
        CHARACTER*200 FUNCTION ERANGE(TEXT)	
Can't return a character value of variable size.
         CHARACTER*(*) TEXT	
         CHARACTER*200 ALINE	
         INTEGER N,N1,N2	
         INTEGER I,I1		
          ALINE = ""		
          L = 0			
          I = 1			
          CALL FORASIGN		
Chug through another number or number - number range.
        R:DO WHILE(EATINT(N1))	
            N2 = N1			
            IF (PASSBY("-")) CALL EATINT(N2)	
            IF (L.GT.0) CALL EMIT(",")		
            DO N = N1,N2,SIGN(+1,N2 - N1)	
              CALL SPLOT(N)				
              IF (N.NE.N2) CALL EMIT(",")		
            END DO				
            IF (.NOT.PASSBY(",")) EXIT R	
          END DO R		
Completed the scan. Just return the result.
          ERANGE = ALINE(1:L)	
         CONTAINS	
          SUBROUTINE FORASIGN	
    1       IF (I.LE.LEN(TEXT)) THEN	
              IF (TEXT(I:I).LE." ") THEN	
                I = I + 1				
                GO TO 1					
              END IF			
            END IF			
          END SUBROUTINE FORASIGN	

          LOGICAL FUNCTION PASSBY(C)	
Could consider or ignore case for letters, but this is really for single symbols.
           CHARACTER*1 C	
            PASSBY = .FALSE.	
            IF (I.LE.LEN(TEXT)) THEN	
              IF (TEXT(I:I).EQ.C) THEN	
                PASSBY = .TRUE.		
                I = I + 1		
                CALL FORASIGN		
              END IF		
            END IF		
          END FUNCTION PASSBY	

          LOGICAL FUNCTION EATINT(N)	
           INTEGER N	
           INTEGER D	
           LOGICAL NEG	
            EATINT = .FALSE.	
            IF (I.GT.LEN(TEXT)) RETURN	
            N = 0			
            IF (PASSBY("+")) THEN	
              NEG = .FALSE.		
             ELSE			
              NEG = PASSBY("-")		
            END IF			
            IF (I.GT.LEN(TEXT)) RETURN	
Chug through digits. Can develop -2147483648, thanks to the workings of two's complement.
   10       D = ICHAR(TEXT(I:I)) - ICHAR("0")	
            IF (0.LE.D .AND. D.LE.9) THEN	
              N = N*10 + D			
              I = I + 1				
              IF (I.LE.LEN(TEXT)) GO TO 10	
            END IF			
            IF (NEG) N = -N		
            EATINT = .TRUE.		
            CALL FORASIGN		
          END FUNCTION EATINT	

          SUBROUTINE EMIT(C)	
           CHARACTER*1 C	
            L = L + 1		
            IF (L.GT.LEN(ALINE)) STOP "Ran out of ALINE
            ALINE(L:L) = C	
          END SUBROUTINE EMIT	

          SUBROUTINE SPLOT(N)	
           INTEGER N		
           CHARACTER*12 FIELD	
           INTEGER I		
            WRITE (FIELD,"(I0)") N	
            DO I = 1,12		
              IF (FIELD(I:I).LE." ") EXIT	
              CALL EMIT(FIELD(I:I))	
            END DO		
          END SUBROUTINE SPLOT	
        END FUNCTION ERANGE	
      END MODULE HOMEONTHERANGE

      PROGRAM POKE
      USE HOMEONTHERANGE
      CHARACTER*(200) SOME
      SOME = "-6,-3--1,3-5,7-11,14,15,17-20"
      SOME = ERANGE(SOME)
      WRITE (6,*) SOME	
      END