Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Perlscript ➜ Global Variables -> Local Variables

Global Variables -> Local Variables

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Solaras   (4 posts)  Bio
Date Tue 14 Jun 2005 03:18 AM (UTC)
Message
I am trying to define my global Variables in mushclient to Local Variables within my sub routine and cannot for the life of figure it out.

Have I mentioned I am new to Mushclient? *hangs his head in shame after 32 years of never learning simple coding"

This is what I have and please someone point out what I am doing wrong.

sub testsub {
my $a = $world->GetVariable("test1");
my $b = $world->GetVariable("test2");
my $c = $world->GetVariable("test3");
if ([$a] == 1)
{
SetVariable([$a], 0)
}
if ([$b] == 2)
{
SetVariable([$b], 0)
}
if ([$c] == 3)
{
SetVariable([$c], 0)
}
}
Top

Posted by Flannel   USA  (1,230 posts)  Bio
Date Reply #1 on Tue 14 Jun 2005 04:25 AM (UTC)
Message
Why are you using square brackets?

You're just trying to access $a, $b, and $c, correct?

and then I understand that you're trying to set test1,2,3 to zero? Since you're not using a hash or anything, you'll need to just hardcode the names into you setvariable statements.
And you will need to use the world object for setting as well.


sub testsub
{
  my $a = $world->GetVariable('test1');
  my $b = $world->GetVariable('test2');
  my $c = $world->GetVariable('test3');
  if ($a == 1)
  {
    $world->SetVariable('test1', 0)
  } 
  if ($b == 2)
  {
    $world->SetVariable('test2', 0)
  }
  if ($c == 3)
  {
    $world->SetVariable('test3', 0)
  }
}

~Flannel

Messiah of Rose
Eternity's Trials.

Clones are people two.
Top

Posted by Solaras   (4 posts)  Bio
Date Reply #2 on Tue 14 Jun 2005 11:45 AM (UTC)
Message
So I cannot modify the local object "a" and have it update the global object "test1"?

I would prefer to have my subroutines work completly with local variables if I can, just calling and assigning them at the start of the routine and everything is set. That way should I ever decide down the road that a=something other than test1, I need only change the identifier, and not the entire code.
Top

Posted by Flannel   USA  (1,230 posts)  Bio
Date Reply #3 on Tue 14 Jun 2005 09:39 PM (UTC)
Message
No, GetVariable merely returns a value, it doesn't return a reference, or anything fancy like that.

You could use a hash, to keep track of the perl variables and their corresponding MC variables, like this:
sub testsub
{
  my %variables = ('a' => 'test1','b' => 'test2', 'c' => 'test3');
  my ($a, $b, $c); #make local variables (my ${$id}) wont work)
  foreach $id (keys %variables)
  {
    ${$id} = $world->GetVariable($variables{$id});
  }
  if ($a == 1)
  {
    $world->SetVariable($variables{'a'},0);
  }
  if ($b == 2)
  {
    $world->SetVariable($variables{'b'},0);
  }
  if ($c == 3)
  {
    $world->SetVariable($variables{'c'},0);
  }
}

I suppose you COULD use tie and things like that to link local variables to mushclient variables, but that seems like an awful lot of overhead.
But once you would have it set up, the local variables and the mushclient variables would be linked for getting and setting.

I don't think you'll be doing enough variable name changing to make either of these things worthwhile, well, maybe at the beginning while you’re getting comfortable with scripting, but once you've gotten the hang of it, all it would do is slow down your scripts. Especially since you won't need to change the variable names at all once you’re more comfortable and your scripts are more mature.

Heck, variables can all be changed with a simple find replace in the world file. Which wouldn't be too much of a hassle for the coder, and would incur no penalty for the user.

~Flannel

Messiah of Rose
Eternity's Trials.

Clones are people two.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


14,602 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.