NAME

Number::Util::Range - Find sequences in number arrays & convert to range (e.g. 100,2,3,4,5,101 -> 100,"2..5",101)

VERSION

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

FUNCTIONS

convert_number_sequence_to_range

Usage:

convert_number_sequence_to_range(%args) -> any

Find sequences in number arrays & convert to range (e.g. 100,2,3,4,5,101 -> 100,"2..5",101).

Examples:

  • basic, non-numbers ignored:

    convert_number_sequence_to_range(array => [100, 2 .. 5, 101, "foo"]); # -> [100, "2..5", 101, "foo"]
  • option: separator:

    convert_number_sequence_to_range(array => [100, 2 .. 5, 101], separator => "-"); # -> [100, "2-5", 101]
  • multiple ranges, negative number:

    convert_number_sequence_to_range(array => [100, 2 .. 6, 101, 102, -5 .. -2, 103]);

    Result:

    [100, "2..6", 101, 102, "-5..-2", 103]
  • option: min_range_len (1):

    convert_number_sequence_to_range(array => [100, 2 .. 5, 101], min_range_len => 5); # -> [100, 2 .. 5, 101]
  • option: min_range_len (2):

    convert_number_sequence_to_range(array => [100, 2, 3, 4, 101, "foo"], min_range_len => 3);

    Result:

    [100, "2..4", 101, "foo"]
  • option: ignore_duplicates:

    convert_number_sequence_to_range(array => [1 .. 4, 2, 9, 9, 9], ignore_duplicates => 1); # -> ["1..4", 9]
  • option: max_range_len (1):

    convert_number_sequence_to_range(array => [98, 100 .. 110, 5, 101], max_range_len => 4);

    Result:

    [98, "100..103", "104..107", 108, 109, 110, 5, 101]

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

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)

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Number-Util-Range.

SOURCE

Source repository is at https://github.com/perlancar/perl-Number-Util-Range.

SEE ALSO

Data::Dump also does something similar when dumping arrays of numbers, e.g. if you say dd [1,2,3,4]; it will dump the array as "[1..4]".

String::Util::Range also convert sequences of letters to range (e.g. "a","b","c","d" -> "a..d").

AUTHOR

perlancar <perlancar@cpan.org>

CONTRIBUTOR

Philippe Bruhat (BooK) <book@cpan.org>

CONTRIBUTING

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.

COPYRIGHT AND LICENSE

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

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

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Number-Util-Range

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.