Well, I'm still new to perl, so its a nice and short code that takes a .txt file and reads line by line removing whitespace adding each character into an array. Then it randomly chooses a character in the array, adding that character to a new array all while replacing it with whitespace in the old as a nice way to filter out the chance of the random num coming back to the whitespace. Well its all good fun. Helped me out with arrays it did!
#!usr/bin/perl
while(<>) {
chomp;
s/\s//g; #Remove whitespace within the line
$leng = length($_);
$count = -1;
while ($count < ($leng - 1) ) {
$count ++;
$char = substr($_,$count, 1);
push @list, $char; #Push one letter into the array
}
}
print "@list\n";
my @list2;
while ( $#list2 < $#list) {
$randint = int(rand($#list+1));
redo if $list[$randint]=~" ";
push @list2, $list[$randint];
$list[$randint]=" ";
}
print "@list2\n";
Enjoy!
**UPDATED BASED ON REPLIES
#!usr/bin/perl
push @list, grep{/\S/} split '' while <>;
print "@list\n";
my @list2;
while (@list){
$randint = int(rand($#list+1));
push @list2, splice @list, $randint, 1;
}
print "@list2\n";
Thanks for all the great ideas!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.