[Techtalk] On Perl

coldfire rolick571 at duq.edu
Fri Jul 26 10:31:32 EST 2002


> I am afraid that I couldn't quite follow this program off Learning Perl. 
> The parts not quite clear to me are:

to be honest, this code is horrible hehe .. i hope it is just for
educational purposes to show possible uses of if, elsif, etc.

> $i = 0;		// the index of the array is set to zero
> $correct = "maybe"; {
> while ($correct eq "maybe")
> {
> if ($words[$i] eq $guess){	// if this index of @words == $guess
> $correct = "yes";		//   $then set correct to yes
> }				//   and break the loop
> elsif ($i < 2) {		// otherwise
> $i = $i + 1;			//  increment the index to check the next 
				//  element in @words

basically, the user inputs a word .. it then runs through a while loop
which will first check to see if the word matches the first element in the
array (@words).  if it doesn't, it checks the next element in the array by
incrementing the index ($i is the index).  @words is an array with three
elements which are referenced as $words[0], $words[1], and $words[2].  if
once it exhausts all elements of the array (when $i is equal to 2, or no
longer less than) it will tell the user they have entered an incorrect
password and then set the index back to 0 and start all over again.

to be honest, this is poorly written code .. so if it's confusing, don't
think it's just you hehe.  if you need any more perl help, let me know :)


coldie

> ----------------------the whole program-----------------------
> 
> #!/usr/bin/perl
> @words = qw (llama camel alpaca);
> print "what is your name\n";
> $name = <stdin>;
> chomp $name;
> if ($name eq "tan") {
> print "hey man\n";
> }
> else
> {
> print "hi, $name\n";
> print "enter the password\n";
> $guess = <stdin>;
> chomp $guess;
> $i = 0;
> $correct = "maybe"; {
> while ($correct eq "maybe")
> {
>   if ($words[$i] eq $guess){
>     $correct = "yes";
>   }
>   elsif ($i < 2) {
>     $i = $i + 1;
>   }
>   else
>   {
>     print "wrong. try again\n";
>     $guess = <stdin>;
>     chomp $guess;
>     $i = 0;
>   }
> }
> }}
> #EOF.
> 
> 
> _______________________________________________
> Techtalk mailing list
> Techtalk at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/techtalk
> 




More information about the Techtalk mailing list