Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

"Ate My Balls" Syntax Generator

by Anonymous Monk
on Apr 15, 2000 at 09:18 UTC ( [id://7689]=sourcecode: print w/replies, xml ) Need Help??
Category: Perl-Fu For Beginners
Author/Contact Info Bowie J. Poag <poag@u.arizona.edu
Description: For those new to Perl, this script uses the ever-popular "Ate My Balls" joke to demonstrate simple concepts like variable assignments, while() loops, printing, simple array manipulation, and the use of Perl's rand() function.
Consider this Chapter 1 in your quest for Perl skillz. I will post more simple scripts like these as I continue to strengthen my "rabid sloth" style of Perl-Fu.


#!/usr/bin/perl
$a=5;                                                                 
+  # Here we make a new variable called "a" to use as a simple loop co
+unter. 
                                                                      
+  # Now, we'll create some arrays, and start them off with some fresh
+ elements.

@group=("swarm","heap","load","gang","squad","bunch","cluster","family
+","team"); # This is an array, called group. Arrays are designated wi
+th in at-symbol (@) prefix.
@adjective=("crack-smoking","supple","furious","sexy","hyperactive","i
+nquisitive","hot","old","crazy");
@noun=("midgets","Cuban refugees","clowns","bitches","programmers","ba
+bies","dogs","hep-cats","apes");

while($a>0)                                                           
+  # This is a simple while-loop construct. It uses the same syntax as
+ Java, or C,
        {                                                             
+  # with one exception...You need to enclose even singular expression
+s in braces.
        $index=rand(9);                                               
+  # This generates a random integer between 0 and 9, and stores it in
+ $index
        print( "\nMr. T says, \"Daaaaammmn!!! A @group[$index]");     
+  # First part of the line is printed here, in this statement.
        $index=rand(9);                                               
+  # A new number is generated, and stored in $index.
        print( " of @adjective[$index]");                             
+  # The n'th element ($index) of the array is printed here.
        $index=rand(9);                                               
+  # A new random number is generated and stored in $index.
        print( " @noun[$index] just ate my balls!!\"");               
+  # The end of the line is printed.
        $a--;                                                         
+  # Our loop counter is decremented here, and the loop iterates.
        }

print("\n\n");                                                        
+  # Toss in a few newlines to keep things pretty, and we're all done!
Replies are listed 'Best First'.
Additionally
by Nitsuj (Hermit) on Jul 27, 2000 at 19:29 UTC
    Additionally, you could modify this code to load the nouns, groups, and adjectives from a file or several files, do a word count on the files for the numbers to put into the rand statement, and run the script that way. You could even write a secondary script to allow users a website using such a script to submit words to the file to be used. GREAT script though, perhaps when I get home from work, I will remember to post my code (very similar) that loads the words from a file (wrote it for my snowboard team's website). Great example, and great theme (gotta love the "Ate My Balls" webring.

    "We're all different!"
    "I'm not"
    -The Life of Brian
Ballsy coding
by Anonymous Monk on Apr 20, 2000 at 20:42 UTC
    Your script works as advertised, and the fact that it's wordier than necessary is obviously deliberate, so that beginners can follow it more easily. The one problem I *do* have with it is that there is no-reason to hard-code to number of elements in each list. Much better to use the $# construct to return the size of the list. In other words:
    print "@groups[rand($#groups + 1)]\n";
    will work even if you add or remove members from @groups.

      Whoops, I'm new here and didn't realize comments were html-ized. That line of code should read:

      print "@groups[rand($#groups + 1)]\n";

      That is all.

        Hmm, even shorter to say: print $groups[rand(@groups)], "\n"; Did the OP mean to take an array slice?

        Or rather:

        print "@groups[rand($#groups + 1)]\n";

        Phew, a "preview post" button would've helped though...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://7689]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-23 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found