Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

The Name Game

by Kozz (Friar)
on Jul 19, 2000 at 22:36 UTC ( #23267=sourcecode: print w/replies, xml ) Need Help??
Category: Fun Stuff
Author/Contact Info Kris Koskelin - kris@angelanthony.com
Description: Just a small bit of code to "do the name game" on any name given. I tried to clean up my code quite a bit to appease the monks before posting it, but I'm sure someone's got some ideas on how to improve it. :-)
#!/usr/bin/perl

print "Content-type: text/html\n\n";
&showform();

if($ENV{'REQUEST_METHOD'} ne "GET"){
  my($myform, $game, $left);
  $myform=&translate_form();
  $$myform{'name'}=~s/(\w)(\w+)/\u$1\L$2/;# capitalize first letter, l
+c the rest
  my @letters=split(//, $$myform{'name'});
  while($letters[0]!~/^[aeiouy]/i){
   last if scalar(@letters)==0;
   shift @letters;
  }
  $game="!name! !name! Bo-B!left!\nBanana Fanna Fo-F!left!\nMe My Mo-M
+!left!\n!name!!\n";
  $$myform{'left'}=join("", @letters);
  $game=~s/!([^!]+)!/$$myform{$1}/sg;
  $game=~s/\n/<br>\n/g;

print <<END;
<HR>
<font face="Comic Sans MS, Verdana, Arial" size=4 color="#FF4444">
$game
</font>
END

}
 print <<END;
</body>
</html>
END


sub showform{
 print <<END;
<html>
<title>The Name Game</title>
<body>
<CENTER>
<form method="POST" action="namegame.cgi">
<input type="text" name="name" size=25>
<input type="submit" value="Name-Game it!">
</form>
</CENTER>
END

}

sub translate_form{
  my($myform, $pair, %myform);
  if ($ENV{'REQUEST_METHOD'} eq "GET"){ 
     $myform = $ENV{'QUERY_STRING'};
  }else{
     read(STDIN, $myform, $ENV{'CONTENT_LENGTH'});
  }  
  foreach $pair (split (/&/, $myform)) {
     $pair=~s/\+/ /g;
     my($name, $value) = split(/=/,$pair);  
     foreach($name, $value){
       s/%(..)/pack ("C", hex ($1))/eg; 
       s/^\s*//sg; # remove leading spaces
       s/\s*$//sg; # remove trailing spaces
     }
     $myform{$name} .= "\0" if (defined($myform{$name})); 
     $myform{$name} .= $value;
  } 
  return \%myform;
}
Replies are listed 'Best First'.
RE: The Name Game
by turnstep (Parson) on Jul 19, 2000 at 23:28 UTC

    Well, you did ask (sort of), so:

    $myform->{'name'}=~s/(\w)(\w+)/\u$1\L$2/; ## You can use perl's builtin functions to do the job: $myform->{'name'}=ucfirst(lc($myform->{'name'});

    You could also replace:

    my @letters=split(//, $myform->{'name'}); while($letters[0]!~/^[aeiouy]/i){ last if scalar(@letters)==0; shift @letters; } $myform->{'left'}=join("", @letters); ## with: ($myform->{'left'}=$myform->{'name'}) =~ s/$[^aeiouy]+//;
Re: The Name Game
by thabenksta (Pilgrim) on May 02, 2001 at 00:57 UTC

    Very nice, this will keep me entertained for hours.

    -thabenksta
    my $name = 'Ben Kittrell'; $name=~s/^(.+)\s(.).+$/\L$1$2/g; my $nick = 'tha' . $name . 'sta';
Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://23267]
help
Chatterbox?
[jedikaiti]: Hi, Monks
[marto]: heya jedikaiti!
[jedikaiti]: Cute Robot!
[jedikaiti]: How's life, marto?
[marto]: I saw this robot the other day, pretty fancy stuff. Things are pretty good jedikaiti, improving :) How are things with you?
[jedikaiti]: Doing OK, but a little achy this AM - I think the cats were using me as a wrestling mat and mattress while I was asleep last night.

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (18)
As of 2017-07-25 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (374 votes). Check out past polls.