Global array not shared between scripts?

Things you’d like to miss in the future...
Forum rules
You might want to read this if you're looking for priority service

:info: Please Include The Following Information
1) Your XYplorer Version (e.g., v28.00.0801)
2) Your Windows Version (e.g., Win 11)
3) Your Screen Scaling Percentage (e.g., 125%).

:info: Use Your Profile Or Signature
We strongly recommend adding your Windows Version and Screen Scaling Percentage to the Location field in your Profile or to your Signature. That way, you only have to type them once, and we won't have to search for that vital information.

:info: How To Attach Images
When attaching an Image, please use the Attachment tab at the bottom of your post and click "Add files".
Post Reply
Norn
Posts: 517
Joined: 24 Oct 2021 16:10

Global array not shared between scripts?

Post by Norn »

I tested the following code in one script:

Code: Select all

    $a[] = array("cat", "dog");
    global $a[];
    text $a[0];
Then, in another script:

Code: Select all

    global $a[];
    text $a[0];
However, $a[0] is undefined in the second script.

According to the documentation, the global command is supported by arrays:
The global command is supported by arrays, eg: global $a[];
Am I misunderstanding how global arrays work, or is this expected behavior?
Windows 11 24H2 @100% 2560x1440

jupe
Posts: 3566
Joined: 20 Oct 2017 21:14
Location: Win10 22H2 120dpi

Re: Global array not shared between scripts?

Post by jupe »

Looks like a bug to me, topic moved.

@Don: If I modify as below then I get a proper err9 dialog too:

Code: Select all

"1"
  global $a[];
  $a[] = array("cat", "dog");
  text $a[0];
  sub "_2";
"_2"
  global $a[];
  text $a[0];

altoclef
Posts: 107
Joined: 12 Oct 2012 18:54
Location: Win11 @100%; XY 27.20.1600

Re: Global array not shared between scripts?

Post by altoclef »

Defining a variable as local and then as global should hide the original local variable. Help says (extraneous details removed):

Code: Select all

msg $foo;   // $foo (uninitialized, not a variable)
 $foo = 15;  // set local value: new local var $foo is created
 msg $foo;   // 15 (local)
 // now make $foo global
 // it's set to 8 (as previously set in script1)
 global $foo;
 msg $foo;   // 8 (global)
A simple example:

Code: Select all

$i = 5;
    global $i; // resets $i to empty
so the surprise to me is not that the global array is blank in the second script but that it isn't in the first script.

(Edited for typos)

altoclef
Posts: 107
Joined: 12 Oct 2012 18:54
Location: Win11 @100%; XY 27.20.1600

Re: Global array not shared between scripts?

Post by altoclef »

In jupe's code the array is not recognised as global once it has been populated, so the array in script "_2" is a different variable.
Screenshot 2026-11-11 134951.png
To see the attached files, you need to log into the forum.

altoclef
Posts: 107
Joined: 12 Oct 2012 18:54
Location: Win11 @100%; XY 27.20.1600

Re: Global array not shared between scripts?

Post by altoclef »

As an aside, we can say:

Code: Select all

global $x = 99;
but not

Code: Select all

global $a[] = array("cat", "dog");   // Call to undefined function: array
Is that intended?

altoclef
Posts: 107
Joined: 12 Oct 2012 18:54
Location: Win11 @100%; XY 27.20.1600

Re: Global array not shared between scripts?

Post by altoclef »

Alternatively, use a function.

Code: Select all

"1"
  $a[] = array("cat", "dog");
  FooBar($a);
  text "Script 1: $a[0] $a[1] $a[2]";

function FooBar(&$array)  // must be passed by reference
{
  text "FooBar: $array[0] $array[1]";
  $array[2] = "mouse";
}


admin
Site Admin
Posts: 67201
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Global array not shared between scripts?

Post by admin »

Should be fixed in next beta.

jupe
Posts: 3566
Joined: 20 Oct 2017 21:14
Location: Win10 22H2 120dpi

Re: Global array not shared between scripts?

Post by jupe »

My last repro looks fixed in .0628+, but a couple other things I noticed, the arrays are never marked as [global] in the Variable Stack dialog, should they be?

Also there still seems some issue, eg. below is another repro that gets a err9.

Code: Select all

  global $a[];
  explode($a[], "cat|dog");
  e $a[0]; 
Additionally, should this work in a single call when using SC array?:

Code: Select all

global $a[] = array("cat", "dog");   // Call to undefined function: array

admin
Site Admin
Posts: 67201
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Global array not shared between scripts?

Post by admin »

All confirmed, fixed the first two. :tup:

Yes, global $a[] = array("cat", "dog"); should work, but that's not supported currently. Must do global $a[]; $a[] = array("cat", "dog");.

Post Reply