Number::Range(3pm)

πŸ“¦
version 0.12 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Number::Range πŸ’–

okay bestie, so Number::Range is this Perl extension that lets you define ranges of numbers and then test whether a number is chillin' inside that range. and like β€” plot twist β€” you can also add to it and delete from it. the range is literally alive. it's giving ✨main character✨. no cap.


✨ SYNOPSIS ✨

use Number::Range;

my $range = Number::Range->new("-10..10,12,100..120");
if ($range->inrange("13")) {
  print "In range\n";
} else {
  print "Not in range\n";
}
$range->addrange("200..300");
$range->delrange("250..255");
my $format = $range->range;
# $format will be '-10..10,12,100..120,200..249,256..300'

🌸 DESCRIPTION 🌸

so Number::Range takes a description of a range, and then lets you test whether a number falls within the range. you can also add and delete from the range. it's the whole package, fr fr. 🫢

πŸŽ€ RANGE FORMAT πŸŽ€

the format for a range is pretty straight forward, bestie. to separate sections of ranges it uses a "," or whitespace. and to actually create the range, it uses ".." to do it β€” much like Perl's own binary .. range operator in list context. iykyk. πŸ’…

πŸ’… METHODS πŸ’…

new

$range = Number::Range->new("10..20","25..30","100");

creates the range object, obviously. it will accept any number of ranges as its input. as many as you want. go off. πŸ‘‘

addrange

$range->addrange("22");

this one also takes any number of ranges as input and adds them to the existing range. growing the range. expanding the empire. ✨

delrange

$range->delrange("10");

yep, same energy β€” this also takes any number of ranges as input and deletes them from the existing range. bye bestie, you're not in the range anymore 😭

inrange

$range->inrange("26"); my @results = $range->inrange("27","200");

this takes one or more numbers and checks if each of them exists in the range.

  • if you pass it a list, and you're in array context, it returns a list of 0's or 1's β€” depending on whether that one was true or false in the list position.
  • if you're in scalar context, it returns a single 1 if all of them are true, or a single 0 if one of them failed.

it's giving all-or-nothing in scalar mode. periodt.

range

$format = $range->range; @numbers = $range->range;

context-dependent queen πŸ‘‘ β€” depending on context this returns either:

  • in list context, an array of all the numbers found in the range.
  • in scalar context, a range string.

size

$size = $range->size;

this returns the total number of entries in the range. the headcount. the vibe check result. πŸ¦‹

rangeList

@rangeList = $range->rangeList;

returns the range as an array list, where each element in the list is an array representing the start and stop points of a range. single element ranges are returned as single element arrays with only on indice.

[
  [10,20],
  [25,30],
  [100] ]

look at those little nested arrays. iconic. 🌈


🎁 EXPORT 🎁

None by default. nope. nada. you get nothing unless you ask. πŸ’€


πŸ‘€ SEE ALSO πŸ‘€

Number::Tolerant, Tie::RangeHash, and Array::IntSpan for similar modules. if Number::Range isn't your vibe, these are the other besties in the group chat. 🫢


🫢 AUTHOR 🫢

Larry Shatzer, Jr., <larrysh@cpan.org> β€” the legend, the icon, the moment. ✨


Copyright (C) 2004-14 by Larry Shatzer, Jr. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

and that's the whole page bestie!! 🌸 this is Number::Range, documented in perl v5.34.0, dated 2022-06-16. mwah, go slay your ranges πŸ’…πŸ’–πŸŽ€

πŸ“¦ where this page comes from