Josh Corbalis wrote:
>
> I've been coding some basic programs learning perl and I'm having some
> problems with the or operators. Here are a couple coding samples if anyone
> knows what I'm doing wrong with this please tell me.
>
> #!/usr/bin/perl
> $gimp=<STDIN>;
> chomp ($gimp);
> if ($gimp eq "gimp" || "gimps") {
> print "working";
> }
> print "\n";
>
> With this one I'm trying to just print out the string "working" if the user
> enters either "gimp" or "gimps". No matter what is entered here it still
> prints out "working" though.
>
> I've also replaced the "||" with "or" in that last example but it does the
> same thing.
>
> I can fix the program with either this bit of code:
>
> #!/usr/bin/perl
> $gimp=<STDIN>;
> chomp ($gimp);
> if ($gimp eq "gimp") {
> print "working";
> } elsif ($gimp eq "gimps) {
> print "working";
> }
> print "\n";
>
> or this one:
>
> #!/usr/bin/perl
> $gimp=<STDIN>;
> chomp ($gimp);
> if ($gimp eq "gimp" || $gimp eq "gimps") {
> print "working";
> }
> print "\n";
>
> Is this a problem with the or operators
No.
> or do I just always have to specify
> $gimp eq "string1" || $gimp eq "string2"?
Yes. Or you could use a regular expression or grep.
$gimp =~ /\bstring[12]\b/
grep $gimp eq $_, 'string1', 'string2'
> I would think you would be able to
> just leave off the second $gimp eq.
Not in Perl5. This may be a feature of Perl6?
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]