[solved] Problems with dereferenced access to array-elements

Things you’d like to miss in the future...
Forum rules
:warnred20: :warnred20: :warnred20: :warnred20: :warnred20: READ THIS AND DO IT!!! :warnred20: :warnred20: :warnred20: :warnred20: :warnred20:

: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: 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: When attaching an Image, please use the Attachment tab at the bottom of your post and click "Add files".

:warnred20: :warnred20: :warnred20: :warnred20: :warnred20: READ THIS AND DO IT!!! :warnred20: :warnred20: :warnred20: :warnred20: :warnred20:
Post Reply
PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

[solved] Problems with dereferenced access to array-elements

Post by PeterH »

OK: short version.

Assume: $p contains the variable-name '$A3' of an array.
For both:

Code: Select all

   $ele2  = *$p[2];   // copy 1 element of array
   *$p[2] = 'X2';   // set 1 element of array
*$p[2] could be interpreted 2 ways, I demonstrate them with ():
(*$p)[2] or *($p[2])
I'd expect (and wish) the first version - as the comments describe.
(The 2nd version doesn't help much, and is simple to emulate.)

For $ele2 = *$p[2]; the element, i.e. [2], is ignored: the complete array is copied.
For *$p[2] = 'X2'; it seems nothing happens. ($A3[2] is not changed)
Last edited by PeterH on 08 Aug 2023 12:49, edited 1 time in total.

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

Re: Problems with dereferenced access to array-elements

Post by admin »

Unfortunately, you are right. Man, you keep me busy. Fixed in the next beta. :tup:

PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Problems with dereferenced access to array-elements

Post by PeterH »

Fortunatually you're going to fix it :D

Waiting ... can't get on till then. :cup:
Bad that I'm always doing things for the first time :oops:

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

Re: Problems with dereferenced access to array-elements

Post by admin »

Just to make sure, this is how it will be:

Code: Select all

    ! Scripting | Arrays: Dereferencing array elements did not work properly. Fixed.
        $a = array("cat", "dog");
          $p = '$a';
          $el  = *$p[1];      // get dereferenced array element
          echo $el;           // dog
          *$p[1] = 'horse';   // set dereferenced array element
          echo $a[1];         // horse

PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Problems with dereferenced access to array-elements

Post by PeterH »

If I interpret it right it is exact what I meant = wanted! :tup:

OK: when you're ready with *everything* else, you could add the logic with () :whistle:

edit: typo

PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Problems with dereferenced access to array-elements

Post by PeterH »

First *test* shows: this problem is solved!

Before trying in productive script: Report a follow-up problem!
(It was there before your change - I didn't want to mix it up :whistle: )

See following example-script: if executed in stepping mode, the call to f() will only report 3 parms - the 4th is totally ignored.
(But: it *is* passed correct - it's only the stepping display! But that's irritating :? )

Code: Select all

   STEP;
   $s3 = "D1 D2 D3 D4";      // tokens
   Explode($A3, $s3, ' ');   // create Array $A3
   f(0, 1, 2, $A3);          // XXX stepping-info for this call shows only 3 parms!

Function f($p0, $p1, $p2, &$p3)   {
	STEP;   // function is only dummy
}

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

Re: Problems with dereferenced access to array-elements

Post by admin »

The source of the problem here is that XY allows $A3 and $A3[] to exist side by side as independent variables. In your code $A3 resolves to "" (empty string). In the next beta it will be shown like this in the step dialog (no mention in change log):

Code: Select all

f()
0
1
2
""
What should have done in your code is to make clear it's the array variable by adding []:

Code: Select all

 f(0, 1, 2, $A3[]);
The step dialog then:

Code: Select all

f()
0
1
2
$A3[]

PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Problems with dereferenced access to array-elements

Post by PeterH »

Isn't this a bit of syntax mismatch :?:

For explode() + implode() the array variable is defined to *not* have []
For array() it defines that both is allowed
And now you say there's a difference between $a and $a[]. :shock:

Problem for me: *the caller* decides, if a parm is string or array - the script tests that and reacts as necessary.
And in this mini-script the (caller's) parm *is* an array - no "" or nothing. (You can check it in the function!)
So no: I don't understand the explanation. :naughty:

By the way I remember I wanted to ask: what happens if I assign a plain variable to an array-variable, or vice versa? I expected overwriting...


Another: can I distinguish indexed and associative arrays?
What if I add $a['text'] to an indexed arrray?

It seems some things are left unspecified...

I'll stop it here (for the moment)

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

Re: Problems with dereferenced access to array-elements

Post by admin »

Relax, you can see it as an additional default element:

Code: Select all

echo $a; // non-array-value
echo $a[0]; // 1st array value
echo $a[1]; // 2nd array value
PS:
For explode() + implode() the array variable is defined to *not* have []
The help file is wrong here. [] is totally tolerated. I'll correct that.

PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Problems with dereferenced access to array-elements

Post by PeterH »

OK: I misunderstood that the function definition should specify the []. (So I wrote that the function doesn't know it before being called.) But it's the parm in the call. That relaxes a lot.

But if $a is non-array, and $a[] is array, it's strange that the function sees vartype($p3) as 'array' if called with $a.
(Don't get me wrong - it's what I want. But I still don't get the logic...)
===> stepping-display says it's (empty) string, while the function sees the expected array - strange :shock:

So what's left is problem with my understanding :roll:

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

Re: Problems with dereferenced access to array-elements

Post by admin »

Each variable has a "base value" (to give it some name) and an optional array of values. If vartype() returns "array" read it as "has also array values". Which value is used depends on how you access the variable. If not explicitly marked as array (by adding []) the step dialog displays the base value.

PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Problems with dereferenced access to array-elements

Post by PeterH »

Thanks for the good explanation :appl:

Still I don't know why the function sees the array (this is a must), while the step dialog doesn't.
I'd say it's important that it's an array, and only "by the way" it also has a 'base' var.
For me it's "not expected". But I can live with that.

PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Problems with dereferenced access to array-elements

Post by PeterH »

Thiink I diidn't mention: tested :arrow: :tup:

Post Reply