Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

"Ate My Balls" Syntax Generator

by Anonymous Monk
on Apr 15, 2000 at 09:18 UTC ( #7689=sourcecode: print w/replies, xml ) Need Help??
Category: Perl-Fu For Beginners
Author/Contact Info Bowie J. Poag <
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.

+  # Here we make a new variable called "a" to use as a simple loop co
+  # Now, we'll create some arrays, and start them off with some fresh
+ elements.

+","team"); # This is an array, called group. Arrays are designated wi
+th in at-symbol (@) prefix.
@noun=("midgets","Cuban refugees","clowns","bitches","programmers","ba

+  # 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.
+  # 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.
+  # A new number is generated, and stored in $index.
        print( " of @adjective[$index]");                             
+  # The n'th element ($index) of the array is printed here.
+  # A new random number is generated and stored in $index.
        print( " @noun[$index] just ate my balls!!\"");               
+  # The end of the line is printed.
+  # Our loop counter is decremented here, and the loop iterates.

+  # Toss in a few newlines to keep things pretty, and we're all done!
Replies are listed 'Best First'.
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?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2023-02-01 01:43 GMT
Find Nodes?
    Voting Booth?

    No recent polls found