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.


In reply to Re^3: Loop walkthrough by roboticus
in thread Loop walkthrough by beanscake

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.