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

I am new to perl and am trying to get what I thought was a simple eq statement to work but something seems to be wrong with my syntax. This is the code I am running
for ($y = 0; $y < $length ; $y++ ) { my($arg); $arg = $_[$y]; if ( length $arg == 1 ) { $and = $and . " AND j$y.LumbolValue = '$arg' " ; } elsif ( $arg eq "vowel\n"){ $and = $and . " AND j$y.Vowel = 1 " ; } elsif ( $arg eq "consonant\n"){ $and = $and . " AND j$y.Consonant = 1 " ; } elsif ( $arg eq "alpha\n"){ $and = $and . " AND j$y.Letter = 1 " ; } else { $and = $and . " /* else: y: $y arg: [$arg] */ " ; } }
On every iteration only the "else" block runs and my output is basically:
/* else: y: 0 arg: [vowel] */ /* else: y: 1 arg: [c] */ /* else: y: 2 +arg: [e] */ /* else: y: 3 arg: [consonant] */
I originally tried writing the eq statements without \n but added those chracters after is supposedly was helpful to someone else. Thanks in advance for any help, I have been stuck on this for over an hour so any help would be extremely welcome.

Replies are listed 'Best First'.
Re: eq statement not working
by roubi (Hermit) on Apr 29, 2009 at 03:07 UTC
    The output from your 'else' statement shows that $arg never contains a string ending with "\n". Remove the \n character from your elsif statement.
      Okay, i found the problem. I was using perl to get my url variables, put them into an array and pass that array to my perl function. This is the code I used to get my url vars:
      $query = new CGI; my @array = (); for $var ( $query->param) { push (@array, $query->b($var) ); }
      Apparently that code adds bold tags to my variables so instead of getting 'vowel' like I thought I should be, i was getting 'vowel' but with '<'b'>' tags. And then when I tried to debug it, everything looked fine because all I saw was that my variable was set to vowel. I recently switched to Ubuntu from windows xp and the fonts in firefox seem slightly different so the fact that it was bold did not stand out greatly to me. And of course, when I copied and pasted the output into the forum, the bold tags were lost anyways.
        Here is a more Perl-ish way to write your code if you're interested:
        my $y = 0; for my $arg (@_){ if ( length $arg == 1 ) { $and .= " AND j$y.LumbolValue = '$arg' " ; # Your elsifs here... } $y++; }
Re: eq statement not working
by Anonymous Monk on Apr 29, 2009 at 03:05 UTC
    What is your INPUT?