Perl and if ( )

Posted by Nid on Fri 25 Aug 2006 07:06 PM — 5 posts, 20,915 views.

Sweden #0
Hey!

I have a trigger, and then this to set summon names:

$summon1 = $world->trim ($world->GetTriggerInfo ($thename, 102));
$summon2 = $world->trim ($world->GetTriggerInfo ($thename, 104));

Now, if $summon1 = $summon2 I want $summon2 to be "$summon2 2"
Using:

if( $summon1 = $summon2 )
{
$world->setvariable("summon2", "$summon2 2");
}

The problem is, it doesn't matter if $summon1 is the same as $summon2 or not, it sets $summon2 to "$summon2 2" anyway.

And when I'm at it, anyone know if theres a "elseif" or "else" for perl?

Anyone know? :)

Thanks in advance!
Amended on Fri 25 Aug 2006 07:07 PM by Nid
USA #1
You need to use '==', not '='. '==' is the equality test, and '=' is an assignment.
Sweden #2
if( $summon1 == $summon2 )
{
$world->setvariable("summon2", "$summon2 2");
}

Did not work either. It always sets the summon2 variable to "summon2 2", no matter if they're the same or not :(
USA #3
try making it do the following after the set operations in your code.

$world->Note($summon1);
$world->Note($summon2);

And are the names correct?
If Not, then you are setting them to incorrect wildcards, if so then I have no idea, it could be the particular method of setting them, I always use variables, as attempting to retrieve a previous wildcard match, I have found to be quite messy and causes more problems than the ease of a few less variables is. Because of the particular execution time or even order of said trigger may occur AFTER the set summonX statements do.

if you can post your entire code I can see what I can do to debug it, perl is not my primary language, but I am learning it rapidly via reading these forums... heheh

Laterzzz,
Onoitsu2
Australia Forum Administrator #4
Ah, Perl. :)

I see what you mean, I tried that and it did it to me too. Had to go fish out my Perl book from the bookshelf. It seems that (unlike Lua) Perl has different comparison operators for strings to numbers. You need to use "eq" and not "==". This test works:


$summon1 = "rat";
$summon2 = "rat";

if( $summon1 eq $summon2 )
{
$summon2 =  "$summon2 2";
}

$world->Note ("summon1 is $summon1");
$world->Note ("summon2 is $summon2");



If you change summon2 to be something other than rat, it does not get a "2" at the end.

You want to be a bit careful too, in not confusing MUSHclient variables with Perl variables. For example, you had:


 $world->setvariable("summon2", "$summon2 2");


Now after this the MUSHclient variable "summon2" has changed, but not the Perl variable $summon2. For simplicity in my example, I just changed the Perl variable.