Generating them isn't a huge cost, and it's just a one-time cost. And if you use a string rather than an array to store it, it's much more efficient, both in time and size.
benchmark for [[:alpha:]] on Windows:
__END__
C:\Users\peter.jones\Downloads\TempData\perl>posix_bench.pl
mem before generate arrays: 11,584 K
mem after generate arrays: 25,688 K
mem after delete arrays: 17,600 K
mem before generate strings: 17,600 K
mem after generate strings: 18,264 K
mem after delete strings: 18,264 K
COMPARE GENERATING:
Rate genArr genStr
genArr 1.27/s -- -92%
genStr 16.5/s 1198% --
COMPARE ACCESSING:
Rate getStr10k getArr10k
getStr10k 7.40/s -- -98%
getArr10k 417/s 5539% --
And practical code for using multiple posix character sets, and generating random strings from those sets:
#!perl
use 5.012; # strict, //
use warnings;
use open ':std', ':encoding(UTF-8)';
my (%posix, %lengths);
for my $class (qw/alpha digit punct/)
{
$posix{$class} .= $_ for grep { /[[:${class}:]]/ } map {chr} 0 ..
+0xEFFFF;
$lengths{$class} = length($posix{$class});
}
sub get_random_char_from_posix
{
my $class = shift;
die "no such class" unless exists $posix{$class} and exists $lengt
+hs{$class};
substr $posix{$class}, rand($lengths{$class}), 1;
}
use Data::Dump;
my $alpha_str; $alpha_str .= get_random_char_from_posix('alpha') for 1
+ .. 10;
my $digit_str; $digit_str .= get_random_char_from_posix('digit') for 1
+ .. 10;
my $punct_str; $punct_str .= get_random_char_from_posix('punct') for 1
+ .. 10;
dd $_ for $alpha_str, $digit_str, $punct_str;
__END__
C:\Users\peter.jones\Downloads\TempData\perl>posix_use.pl
"\x{288EC}\x{1309F}\x{88F8}\x{29B5D}\x{85A3}\x{209E2}\x{9EE1}\x{23015}
+\x{168AB}\x{2A691}"
"\x{B6D}\x{17E5}\x{1D7F6}\x{A627}\x{F21}\x{6F1}\x{1043}\x{A8D7}\x{118E
+4}\x{116C2}"
"\x{A673}\x{FE16}\x{110BC}\x{2CFF}\x{1BFE}\x{1804}\x{11642}\x{1AA0}\x{
+2051}\x{1183B}"
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.