Getting the right argument at the right place

Posted by Martijn on Sat 26 May 2007 06:54 PM — 10 posts, 36,607 views.

#0
I am having some trouble getting the right part of the matched text send to a script. What I am trying now is the following:


    <alias
    script="target_tester"
    match="^target (\w+)"
    enabled="y"
	ignore_case="y"
	regexp="y"
	>
    
   </alias>


with this script (in perlscript)

sub setTarget{
  my $i = 0;
  for my $arg (@_){
    $world->note("arg ".$i." is ".$arg);
	$i++;
  }
}

when I put in "target bigbadwolf" I expected the following to come out:

arg 0 is target bigbadwolf
arg 1 is bigbadwolf


what comes out is:

arg 0 is 
arg 1 is target bigbadwolf
arg 2 is ARRAY(0xf8ed9c)


what I want is to get that argument bigbadwolf, but I can't seem to get it. I'm probably doing it completely wrong, and I would really appreciate a hand.

Australia Forum Administrator #1
Take a look at the examples in exampscript.pl file that comes with MUSHclient.

For example, getting a wildcard for an alias:


sub OnAddTeleport 
{
my ($thename, $theoutput, $wildcards) = @_;

# wildcard 2 is the room name

$sDestination = $world->trim ($world->GetAliasInfo ($thename, 102));


...


I think I had trouble accessing wildcards from the array that is passed down to the function, so I made an extra way of getting at them, namely:

http://www.gammon.com.au/scripts/doc.php?function=GetAliasInfo

You can now also use:

http://www.gammon.com.au/scripts/doc.php?function=GetAliasWildcard

So, the simple answer is, assuming you have a name for your alias (in the Label field) then all you would have to do is call GetAliasWildcard, using that name, and the wildcard number you want.
#2
Once again, Nick Gammon to the rescue! So from what I gather, the array of wildcards is not send to the script, but is instead stored in an internal datastructure, and I can get them out again one by one and get them in a new array with (for perlscript again)


<alias
 script="argument_tester"
 match="^(match) (the) (big) (bad) (wolf)"
 enabled="y"
 ignore_case="y"
 regexp="y"
 name="argument_tester"
></alias>

sub argument_eater{
  my $name = shift;
  my $output = shift;
  my $wildcards = shift;
  my @arguments = ();
  for (my $i = 0; push (@arguments GetAliasWildcard ($name, $i);$i++;){}
  }
  for my $argument (@arguments){
    $world->note("argument is ".$argument);
  }
}


For now this will more than suffice for me. The only difficulty I might still run in to, is that this only works if argument_eater would be called from an alias. If it is called from a Trigger it won't work, as I would need GetTriggerWildcard. If I would call it from another script method, it would pass the array as an argument. I could make a seperate function for that one, i.e. have a trigger_argument_eater an alias_argument_eater and a sub_argument_eater. Do you know of any workarounds? It would be especialy nice if I could access the built in array, if I could name the array in the trigger/alias and pass the arrays name to the function, or even access the internal array just by its memory adress (I assume the ARRAY(0xf8ed9c) is the memory adress of the internal array), but I could also imagine you have some sort of built in function, (I couldn't find it, but I think I proved my skills in finding the proper function are lacking somewhat) that determines the caller of the script, and if it is an Alias or a Trigger.

Anyway, thank you again for the response, in by far the most cases, I'll be able to do exactly what I want now.
Australia Forum Administrator #3
There is a GetTriggerWildcard function, as I gather you noticed.

This seems to be a limitation of Perl, or my knowledge of it anyway.

The array is indeed passed to your script function ($wildcards in my example), however I had trouble accessing it, as it is internally a VBscript Variant Array type.

In most languages (eg. VBscript, JScript, Lua) I had no trouble accessing the wildcard directly from the array supplied as the third argument.
#4
with my limited knowledge of OLE/COM objects I thought activePERL would magicaly poof it and make everything allright. It seems not. Even my targetting system, which was working properly before has gone dodgy now. I have it now as

<aliases>
   <alias
    script="setTarget"
    match="^target (\w+)"
    enabled="y"
	ignore_case="y"
	regexp="y"
	name="targetting"
   ></alias>
</aliases>
<script>
<![CDATA[

sub setTarget{
  my ($theName, $theOutput, $wildcards) = @_;
  my $target = GetAliasWildcard($theName, "1");
  $world->note("setting target to ".$target);
  $world->setVariable("target",$target);
  my $fetchedTarget = $world.getVariable("target");
  $world->note("target is now ".$fetchedTarget);
}
]]>

as it is now, imputting
target bigbadwolf
should output

setting target to bigbadwolf
target is now bigbadwolf
right?
wrong. It gives

setting target to bigbadwolf
target is now Win32::OLE=HASH(0xfaa1b4)bigbadwolf 


I'm confused now...
#5
quick note to keep the spirits up: note how 0xfaa1b4 is almost hex: Fail b4.

Okay, that was a little lame.
Australia Forum Administrator #6
Yes, it failed before. :)

Quote:

setting target to bigbadwolf
target is now Win32::OLE=HASH(0xfaa1b4)bigbadwolf


Ah, Perl. :P

Maybe this will work:


$world->setVariable("target", "$target");

#7
Alas. I'm going to have to learn VB, Jscript or Lua I guess... Or might it do any good to drop a crosspost in the Perlscript section?
Australia Forum Administrator #8
Could be worth a try.
#9
Fortunately, I haven't scripted that much yet, and switching to Python for now have given my no such dificulties yet. I'll still put down a crosspost, and see if there are any fixes or workarounds.