Recursive Subroutine permute | Basic Permutations

Prince

[ Verified Seller ]
Staff member
Trusted Seller
Joined
11 yrs. 6 mth. 26 days
Messages
5,381
Reaction score
18,380
Age
45
Wallet
11,590$
Simple Perl Script:

Code:
Please, Log in or Register to view codes content!

[*] The above script prints out all permutations of the strings supplied as command-line arguments. Example of run:
[*] The above script prints out all permutations of the strings supplied as command-line arguments. Example of run:

Code:
Please, Log in or Register to view codes content!

[*] Output:
Code:
Please, Log in or Register to view codes content!

[0x03] Script Functions:

[*] Perl Unshift Function: Places the elements from LIST, in order, at the beginning of ARRAY. This is opposite function to shift().

[*] Return Value: Number of new elements in ARRAY.
[*] Example:
Code:
Please, Log in or Register to view codes content!

[*] Output:
Code:
Please, Log in or Register to view codes content!

[*] Perl Splice Function: Removes the elements of ARRAY from the element OFFSET for LENGTH elements, replacing the elements removed with LIST, if specified. If LENGTH is omitted, removes everything from OFFSET onwards.

[*] Return Value:In scalar context undef if no elements removed
In scalar context last element removed
In list context empty list on failure
In list context list of elements removed
[*] Example:
Code:
Please, Log in or Register to view codes content!

[*] Output:
Code:
Please, Log in or Register to view codes content!

[0x04] Pimping The Script & Output:

Now of course, Im sure you must have a simple underatanding of the above script and how the functions work. So why not just edit it yourself and make it handle more. For example, instead of it handling words given via command line arguments, edit it to read a text file of words and shift them around to create new words. Use some bash commands to add prefixes and suffixs to each line, digits, padding techniques, bash brace expessions, use text editors such as sed, awk, gawk, nawk to edit lines. The possiblities are pretty much endless.

[*] Example of above code being pipped to sed:
Code:
Please, Log in or Register to view codes content!

[*] Output:
Code:
Please, Log in or Register to view codes content!

[0x05] Conclusion:

Well thats my little short tutorial to string permutation in perl. Hope this helps those who are interested in this languge! Claps for first perl thead :3
 
Top Bottom