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

Greetings, fellow monks.

I'm having a problem with a sub that receives an array as parameter and should make a decision based on the values of each element of this array.

My problem is that it doesn't make the right decision. So, either I'm being very blind and stupid or my perl interpreter is going nuts. I know that the first option seems to be the right one, so I ask for your wisdom and patience.

The sub is called by this line:
$status_chg = DevelopUtils::check_status(@status_atividades);
and in the DevelopUtils.pm file there is the corresponding sub, wich is:
sub check_status { @status_atividades = @_; $status_possivel=""; $l = 0; foreach $status(@status_atividades) { if ($l < 1) { if ($status eq "1" or $status eq "9") { $status_possivel .= "<option value=\"2\">Cancelada</op +tion>\n"; $status_possivel .= "<option value=\"3\">Agendada</opt +ion>\n"; $l++; } elsif ($status eq "3" or $status eq "9") { $status_possivel .= "<option value=\"4\">Em Análise Fu +ncional</option>\n"; $l++; } } return($status_possivel); } }
The problem is that I always get only the first option of the if part as if it is the only option available.

Please, what the hell am I doing wrong on this???

Thanks in advance,

Er Galvão Abbott
a.k.a. Lobo, DaWolf
Webdeveloper

Replies are listed 'Best First'.
Re: Can't get the right array value
by Masem (Monsignor) on Nov 07, 2001 at 01:54 UTC
    You short-circuit any addition appending with this line:
    if ($l < 1)
    since after you match once, $l is 1, and thus fails. Take this test out.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

Re: Can't get the right array value
by runrig (Abbot) on Nov 07, 2001 at 01:50 UTC
    Please, what the hell am I doing wrong on this???

    Well, for one thing, you (repeatedly) ask questions without first trying use strict and warnings.

    Now, in this case, it won't directly help find the problem, but it might get you to think more about exactly how your using your variables which might make the problem more clear.

    And with variable names like @status_atividades, I would definitely be using strict.

Re: Can't get the right array value
by danboo (Beadle) on Nov 07, 2001 at 01:59 UTC
    If you intend for it to make a decision based on "each element of this array", then the decision making code (if..elsif) needs access to all of the elements. Currently it stops iterating because the return is inside the foreach block. Once you've changed this you still need to change some logic regarding the "if ($l < 1)" block. After the first match this block will be skipped because $l is incremented and no longer less than 1. If I understand you correctly, then what you need is:
    sub check_status { @status_atividades = @_; $status_possivel=""; foreach $status(@status_atividades) { if ($status eq "1" or $status eq "9") { $status_possivel .= "<option value=\"2\">Cancelada</op + +tion>\n"; $status_possivel .= "<option value=\"3\">Agendada</opt + +ion>\n"; } elsif ($status eq "3" or $status eq "9") { $status_possivel .= "<option value=\"4\">Em Análise Fu + +ncional</option>\n"; } } return($status_possivel); }
    cheers, - danboo
Re: Can't get the right array value
by XPWhore (Novice) on Nov 07, 2001 at 02:17 UTC
    Hate to get on any negative bandwagon, but have you heard of scoped variables?

    use strict; will force you to be a better programmer.

      Ok, Ok... I said that I was being stupid.

      I just wanna explain something: I use a perl editor that gives me the code errors in "real time", so I don't need to use the -w flag. BTW, this flag is really annoying since I end up with tons of "Possible typo" messages, because this application uses a lot of html templates.

      And like the other guy said, the -w flag wouldn't help in this case. To be honest it never helped me, since the editor makes me see alomost all the code errors.

      Well, I admit that I don't use strict because I don't fully understand it yet, but I promise I'll make some time to take a look on it.

      Thanks for your help anyway, guys.

      Er Galvão Abbott
      a.k.a. Lobo, DaWolf
      Webdeveloper
        Aw, shucks... I didn't meen to make you feel stupid.

        And if you're using a perl editor that can tell you when you make a mistake by 'misnaming' an undefined variable, lemme know what it is, I'd like to use it. My most common coding mistake is failing to type var names correctly.
        One could argue that I should use simpler var names or put all vars and their description at the top of the closure, but while I've never been known as a lazy documenter, I usually only add comments to my code when I'm doing something that is odd or to explain a funky regex.

        And strict is something you want to use always. Among other things, it forces you to declare your variable names before using them.

        It'll save you time when you debug during development.

        As for -w, well once you understand why you should use it, then you know when you can not use it. IMHO, all people in the early stages of perl should use warnings. Once you become more experienced you prolly wont use it as much, if at all.

        $0.02 doesn't go as far as it used to, does it?

Re: Can't get the right array value
by danboo (Beadle) on Nov 07, 2001 at 02:08 UTC
    One more thing... The ' eq "9"' test in your elsif statement is pointless. If status is 9 then the first test will catch it and the second test will be skipped. Perhaps you want that to just be: if ($status eq "3" or $status eq "9") - danboo
      The "eq 9" thing looks pointless, but since the app will have tons of other elsif's it wont be pointless in a near future.

      Er Galvão Abbott
      a.k.a. Lobo, DaWolf
      Webdeveloper
Re: Can't get the right array value
by Biker (Priest) on Nov 07, 2001 at 02:10 UTC

    Adding to all the other comments given, you may want to think a bit of your if-statement on $status. You compare $status to "9" both in your 'if' and in your 'elsif' which is probably not what you want.

    f--k the world!!!!
    /dev/world has reached maximal mount count, check forced.