Re: Is substr only way of getting nth character of string?

Re: Is substr only way of getting nth character of string?

am 04.04.2008 21:25:15 von sheinrich

On Mar 21, 1:55 am, "Robbie Hatley"
wrote:
> New version:
>
> #permute.perl
> use strict;
> use warnings;
> sub rand_int
> {
> my ($min, $max) = @_;
> return int ( $min + rand ( $max - $min + 0.999 ) ) ;}
>
> srand;
> my @Charset = map chr, 9, 32..126;
> my @TempCharset = @Charset;
> my @PermCharset;
> while (@TempCharset > 0)
> {
> my $RandomInt = rand_int(0, @TempCharset - 1);
> my $RandomChar = $TempCharset[$RandomInt];
> push @PermCharset, $RandomChar;
> splice @TempCharset, $RandomInt, 1;}
>
> print @PermCharset, "\n";
>
Better late than never.
This one is my favourite shuffling algo. It's called a 'fisher yates
shuffle' and perl allows to swivel elements in-place:

use strict;
use warnings;

my @array = 1..100;

for (my $i = @array; $i; ) {
my $j = int rand $i--;
next if $i == $j;
@array[$i,$j] = @array[$j,$i];
}

print "@array\n";


Give it a shot,
steffen

Re: Is substr only way of getting nth character of string?

am 05.04.2008 01:15:41 von Tad J McClellan

sheinrich@my-deja.com wrote:


> This one is my favourite shuffling algo. It's called a 'fisher yates
> shuffle'


That algorithm is given in the answer to this Frequently Asked Question:

perldoc -q shuffle


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: Is substr only way of getting nth character of string?

am 05.04.2008 13:31:46 von Sherm Pendley

Tad J McClellan writes:

> sheinrich@my-deja.com wrote:
>
>
>> This one is my favourite shuffling algo. It's called a 'fisher yates
>> shuffle'
>
>
> That algorithm is given in the answer to this Frequently Asked Question:
>
> perldoc -q shuffle

Also, in the Perl Cookbook.

sherm--

--
My blog: http://shermspace.blogspot.com
Cocoa programming in Perl: http://camelbones.sourceforge.net