Array::Pick::Scan - Pick random items from an array (or iterator), without duplicates
This document describes version 0.005 of Array::Pick::Scan (from Perl distribution Array-Pick-Scan), released on 2022-05-21.
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});
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.
shuffle
samples
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.
Older name for "pick", deprecated and will be removed in future releases.
Please visit the project's homepage at https://metacpan.org/release/Array-Pick-Scan.
Source repository is at https://github.com/perlancar/perl-Array-Pick-Scan.
File::Random::Pick uses a similar algorithm.
List::Util's shuffle, List::MoreUtils's samples, List::AllUtils's sample.
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.
perlancar <perlancar@cpan.org>
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.
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.
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.
To install Array::Pick::Scan, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Array::Pick::Scan
CPAN shell
perl -MCPAN -e shell install Array::Pick::Scan
For more information on module installation, please visit the detailed CPAN module installation guide.