On Wed, Oct 22, 2008 at 4:32 AM, Rob Dixon <[EMAIL PROTECTED]> wrote:
> Sharanbr wrote:
>> On Oct 19, 6:38 am, [EMAIL PROTECTED] (Sisyphus) wrote:
>>> On Oct 17, 3:04 am, [EMAIL PROTECTED] (Sharan Basappa) wrote:
>>>>
>>>> #!/usr/bin/perl
>>>> use warnings;
>>>> use Algorithm::Permute;
>>>> my @array = (1..4);
>>>> Algorithm::Permute::permute { print "@array\n" } @array;
>>>
>>> use warnings;
>>> use strict;
>>> use Algorithm::Permute;
>>>
>>> my @array = (1..9);
>>> my $p = new Algorithm::Permute([EMAIL PROTECTED]);
>>>
>>> # print out the first 20 permutations of @array,
>>> # assigning each permutation to @new, and
>>> # printing it out:
>>> for(1..20) {
>>>   my @new = $p->next;
>>>   print "@new\n";
>>>
>>> }
>>
>> I have modified the code a little bit to suit my requirements. But
>> still the code does not seem to work i.e.
>> the final print of @x does not display any value. However, I change
>> the code foreach (@array) to for (1..)
>> the way you have coded, it works fine. My requirement is to put all
>> the permutations into a new array,
>> not just (1..20)
>>
>> I have another basic doubt. After permute is called with @array
>> argument, does it now contain
>> new permutations or still (1..4).
>>
>> #!/usr/bin/perl
>> use warnings;
>> use Algorithm::Permute;
>>
>> my @array = (1..4);
>> my $p  = new Algorithm::Permute([EMAIL PROTECTED]);
>> foreach (@array)
>> {
>>   my @x = $p->next;
>>   print "@x \n";
>> }
>
> You shouldn't pass a real array to the 'new' method as it destroys the array.
> It's bad but there it is, and the documentation does show it being called with
> an anonymous array.
>
> I'm not sure what you mean by 'put all the permutations into a new array', as
> each permutation is held in an array and I'm guessing that you don't know 
> about
> arrays of arrays?
>
> The program I've written below stores each permutation as a single string with
> spaces between the elements. If you really do want a array of arrays instead 
> of
> an array of strings then it's very obvious how to modify it to do that.
>
> HTH,
>
> Rob
>
>
> use strict;
> use warnings;
>
> use Algorithm::Permute;
>
> my $p = Algorithm::Permute->new([1 .. 4]);
>
> my @permutations;
>
> while (my @array = $p->next) {
>  push @permutations, "@array";
> }
>
> print "$_\n" foreach @permutations;
>
Thanks, Rob. The method works and also fits in the overall code I am writing.
The main reason for confusion from my side is my lack of understanding
of how different methods
of algo permute work (e.g. I was not aware that an anonymous array was
created. I was thinking that
the original array is modified to create permutations. Lack of good
doc also contributed to this confusion.

Regards

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to