beanscake has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
In an interview to be processed by any of the available staff , namely patrick or Juliet ,to interview two job applicant namely , James and John .
Begins: if James or John graduated from college with grade A then, employ. One , my default choice is James , but if James did not then employ john. If both has grade A then go for john, no job for them if none of them graduated with grade A ..... This is my scenario Please perl monks help me with infinite loop for this scenario..

Replies are listed 'Best First'.
Re: Loop walkthrough
by roboticus (Chancellor) on Jul 21, 2014 at 06:46 UTC

    beanscake:

    You didn't show the code with the infinite loop, so it would be hard to tell how to fix it.

    Don't worry if it looks ugly--when you show code, you give us the opportunity to teach you better ways of doing things. Also, showing your code gives us a handle on what your thought process is. If you've got a misconception, we may be able to help you through it. But as it is now, we can only guess (and probably poorly).

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      here is the statement please run perl em.pl b b

      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;

        beanscake:

        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.

Re: Loop walkthrough (beanscake FizzBuzz)
by Anonymous Monk on Jul 21, 2014 at 06:54 UTC

    Please perl monks help me with infinite loop for this scenario..

    I'd be willing to write the loop, if you'd be willing to write one for me :)

    Print the first one hundred numbers. If number is divisible by three without remainder print Beans, by five print Cake, by fifteen print BeansCake.