The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Text::NumericList - Perl extension for converting strings into arrays of positive integers and vice-versa.


  use Text::NumericList;
  my $list = Text::NumericList->new;

Converting arrays into strings

  my $string = $list->get_string;   # Returns '1-3,5-7'

  $string = $list->get_string;      # Returns '1-3;5-7'

  $string = $list->get_string;      # Returns '1..3;5..7'

Converting strings into arrays

  my @array = $list->get_array;     # Returns (1,2,3,5,6,7)

  $list->set_range_regexp('.+');    # Set range regular expression to
                                    # a single or multiple periods
  @array = $list->get_array;        # Returns (1,2,3,5,6,7) 

Additional methods

  my $mask = new Text::NumericList;
  $list->set_mask($mask);           # Set the list mask

Mask limits the numeric list to certain values, so that get_string() and get_array() methods return intersection of the current list and the mask:

  @array    = $list->get_array;     # Returns (20,21,22,23,24,25)
  $string   = $list->get_string;    # Returns '20-25'

The following shortcuts work if the mask is set.

  @array    = $list->get_array;     # Returns (15,17-19,21,23,25,27,29)

  @array    = $list->get_array;     # Returns (16,18,20-22,24,26,28,30)

  $string   = $list->get_string;    # Returns '15-30'


This class is useful for applications where a user is required to enter large arrays of integers. This class parses user-provided text strings and converts them into arrays of integers. This class also converts arrays of integers into a text string replacing sequential integers with a range (e.g. "1-5").



Creates a new Text::NumericList object.


Sets the 'array' attribute. Converts array into string and sets the 'string' attribute as well.

 Conversion algorithm:

 * In each array element, remove all non-digit characters.
 * Remove non-unique elements.
 * Sort the array.
 * Set the 'array' attribute.
 * Apply the mask to the array.
 * Replace 3 and more sequential elements with a range string (e.g.
   '1-5') using range_symbol attribute.
 * Join elements and with the output_separator string.
 * Set the 'string' attribute.

Converts the string into an array. Sets the 'array' and 'string' attributes to the resulting array using set_array(). Every effort is made to make sense of user input. Unrecognizeable characters are removed from the string. Some illegal combinations of other symbols are removed or replaced. Reverse ranges (such as 8-5) are ignored. If the mask is set, set_string() method recognizes the following keywords: 'all', 'odd', and 'even'.

 Examples (assuming that mask is set to '1-10'):

 Input string   Resulting array     Resulting string        
 '3,2,1,3,5-7'  1,2,3,5,6,7         '1-3,5-7'
 '2,oDD'        1,2,3,5,7,9         '1-3,5,7,9'
 'eVen,5'       2,4,5,6,8,10        '2,4-6,8,10'
 '5,-4-,7'      4,5,7               '4,5,7'
 '7-9-12'       7,8,9,10            '7-10'
 '1 -&% 4s7'    1,2,3,4,7           '1-4,7'

Returns the array attribute.


Returns the string attribute.


Argument: a string. Sets range regular expression. Default is '\-'.


Argument: a string. Set the combination of characters to present ranges in the output string. Default is '-'.


Argument: a string. Sets the string to separate elements in the output string. Default is ','.


Argument: a string. Sets the string which is considered an element separator in the input string. Default is ','. In general, elements in the input string can be separated by any non-digit character which does not match range regular expression. Input separator is used only to avoid character combinations like '5-,' in the input strings.


Argument: another Text::NumericList object. When the mask is set, the values of the 'array' attribute are limited to those specified in the mask object. All other values in the input string or input array are ignored.


Methods set_mask(), set_input_separator(), set_output_separator(), set_range_regexp(), and set_range_symbol() are supposed to be used prior to set_array() or set_string(). The output may not look as expected if you use these methods after set_array() or set_string(), although some effort has been made to make the output look reasonable.


Arkady Grudzinsky, <>