P is for Practical | |
PerlMonks |
A Random Fortune Cookieby choroba (Cardinal) |
on Mar 10, 2021 at 09:03 UTC ( [id://11129395]=CUFP: print w/replies, xml ) | Need Help?? |
I follow the #perl hashtag on Mastodon. Few days ago, someone asked: > Trying to randomize entries in a fortune text file. Not the output--the text of the human-readable fortune file. "sort -R" works on individual lines, but I want to grab multiline entries delimited by %. To which someone else replied > I figured it would be a two step solution. Step 1: install perl. Another person reacted with > <3 #perl. If you'd use a Perl one-liner let me know, I'll see if I can get one working. :) The hashtag in this reply was why I was informed about the discussion. Here's a sample from a typical fortune file (from ruanyf/fortunes):
And here's my solution:
Update: As noted by hippo, the original request was more probably just to randomly sort the whole file. To do that, just remove the [0]. Update 2: Unfortunately, there's the trailing empty cookie problem.
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Back to
Cool Uses for Perl
|
|