NAME
Array::Pick::Scan - Pick random items from an array (or iterator), without duplicates
VERSION
This document describes version 0.005 of Array::Pick::Scan (from Perl distribution Array-Pick-Scan), released on 2022-05-21.
SYNOPSIS
use Array::Pick::Scan qw(pick);
my $item = pick(\@ary);
my @items = pick(\@ary, 3);
or:
my $item = pick(\&iterator);
my @items = pick(\&iterator, 3);
To return array indexes instead of the items:
my @item = pick($source, $n, {pos=>1});
DESCRIPTION
This module can return random items from an array (or iterator), without duplicate elements (i.e. random sampling without replacement). It uses the same algorithm as File::Random::Pick, which in turn uses a slightly modified version of algorithm described in perlfaq ("perldoc -q "random line"")), but uses items from an array/iterator instead of lines from a file(handle).
Performance-wise, this module is inferior to List::Util's shuffle
or List::MoreUtils's samples
, but can be useful in cases where you have an iterator and do not want to put all the iterator's items into memory first.
FUNCTIONS
pick
Usage:
my @items = pick(\@ary [ , $num_samples [ , \%opts ] ]);
my @items = pick(\&iterator [ , $num_samples [ , \%opts ] ]);
Number of samples defaults to 1.
Options:
pos
Bool. If set to true, will return array indexes instead of the items.
random_item
Older name for "pick", deprecated and will be removed in future releases.
HOMEPAGE
Please visit the project's homepage at https://metacpan.org/release/Array-Pick-Scan.
SOURCE
Source repository is at https://github.com/perlancar/perl-Array-Pick-Scan.
SEE ALSO
File::Random::Pick uses a similar algorithm.
List::Util's shuffle
, List::MoreUtils's samples
, List::AllUtils's sample
.
Array::Sample::SimpleRandom provides random sampling without replacement (same as picking in this module) as well as with replacement (creating possible duplicate items).
Array::Sample::WeightedRandom lets you add weighting to each item.
AUTHOR
perlancar <perlancar@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, and sometimes one or two other Dist::Zilla plugin and/or Pod::Weaver::Plugin. Any additional steps required beyond that are considered a bug and can be reported to me.
COPYRIGHT AND LICENSE
This software is copyright (c) 2022, 2020 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=Array-Pick-Scan
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.