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


String::Util::Range - Find sequences in arrays & convert to range (e.g. "a","b","c","d","x",1,2,3,4,"x" -> "a..d","x","1..4","x")


This document describes version 0.002 of String::Util::Range (from Perl distribution String-Util-Range), released on 2023-09-08.




 convert_sequence_to_range(%args) -> any

Find sequences in arrays & convert to range (e.g. "a","b","c","d","x",1,2,3,4,"x" -> "a..d","x","1..4","x").


  • basic:

     convert_sequence_to_range(array => [1 .. 4, "x", "a" .. "d"]); # -> ["1..4", "x", "a..d"]
  • option: min_range_len (1):

     convert_sequence_to_range(array => [1, 2, 3, "x", "a", "b", "c"], min_range_len => 3); # -> ["1..3", "x", "a..c"]
  • option: min_range_len (2):

     convert_sequence_to_range(array => [1 .. 4, "x", "a" .. "d"], min_range_len => 5); # -> [1 .. 4, "x", "a" .. "d"]
  • option: max_range_len:

       array => [1 .. 7, "x", "a" .. "g"],
       max_range_len => 3,
       min_range_len => 3


     ["1..3", "4..6", 7, "x", "a..c", "d..f", "g"]
  • option: separator:

     convert_sequence_to_range(array => [1 .. 4, "x", "a" .. "d"], separator => "-"); # -> ["1-4", "x", "a-d"]
  • option: ignore_duplicates:

       array => [1 .. 4, 2, 9, 9, 9, "a", "a", "a"],
       ignore_duplicates => 1


     ["1..4", 9, "a"]

This routine accepts an array, finds sequences in it (e.g. 1, 2, 3 or aa, ab, ac, ad), and converts each sequence into a range ("1..3" or ""). So basically it "compresses" the sequence (many elements) into a single element.

What determines a sequence is Perl's autoincrement magic (see the perlop documentation on the Auto-increment), e.g. 1->2, "aa"->"ab", "az"->"ba", "01"->"02", "ab1"->"ab2".

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • array => array[str]

    (No description)

  • ignore_duplicates => true

    (No description)

  • max_range_len => posint

    Maximum number of items in a sequence to convert to a range. Sequence that has more than this number of items might be split into two or more ranges.

  • min_range_len => posint (default: 4)

    Minimum number of items in a sequence to convert to a range. Sequence that has less than this number of items will not be converted.

  • separator => str (default: "..")

    (No description)

Return value: (any)


Please visit the project's homepage at


Source repository is at


Data::Dump also does something similar, e.g. if you say dd [1,2,3,4,"x","a","b","c","d"]; it will dump the array as "[1 .. 4, "x", "a" .. "d"]".

Number::Util::Range which only deals with numbers.


perlancar <>


To contribute, you can send patches by email/via RT, or send pull requests on GitHub.

Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:

 % prove -l

If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me.


This software is copyright (c) 2023 by perlancar <>.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


Please report any bugs or feature requests on the bugtracker website

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.