in reply to Re^2: Loop walkthrough
in thread Loop walkthrough
There's no loop in your code. Infinite loop means that your program continues to process forever, and that you need to force-close the application. When the application is run with the arguments you specify, it completes and the error message is:
Use of uninitialized value $employ in print at 1094481.pl line 31
Since you've got a series of if/else clauses it simply means that execution took a path where $employ is never set to a valid value. If you uncomment your uncommented lines, then the code works much better. Then you only need to find and fix any missing cases.
Here's an updated version of your program. I tweaked it a little, but the main reason is to add a "bad" value of $employ on initialization. (If a value is supposed to always be set in a subroutine, I like to put something *obviously* bad in during initialization so I notice any possible problems.) The updated code:
roboticus@sparky:~$ cat 1094481.pl #!/usr/bin/perl use strict; use warnings; my $employ = 'FOOBAR'; #------- my $james = $ARGV[0]; my $john = $ARGV[1]; if ($james =~ m/a/) { $employ = "james with grade a"; #pick james }elsif ($john =~ m/a/) { $employ = "John with grade a"; # pick john } elsif ($james =~ m/b/) { $employ = "james doesnt have grade a"; } elsif ($john =~ m/b/) { $employ = "john doesnt have grade a"; } elsif ($james =~ m/b/ && $john =~ m/b/) { $employ = "Both james and john doesnt have grade a \n"; } print $employ, "\n"; roboticus@sparky:~$ perl 1094481.pl b b james doesnt have grade a
Note: it's easy to make this code print "FOOBAR", so be sure to run various test cases to find these cases, and then come up with an appropriate fix.
...roboticus
When your only tool is a hammer, all problems look like your thumb.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Loop walkthrough
by beanscake (Acolyte) on Jul 21, 2014 at 14:18 UTC | |
|
Re^4: Loop walkthrough
by beanscake (Acolyte) on Jul 22, 2014 at 16:17 UTC | |
by roboticus (Chancellor) on Jul 22, 2014 at 17:44 UTC | |
by Anonymous Monk on Jul 22, 2014 at 19:50 UTC |