Page 2 of 3

Re: To array definitions

Posted: 16 Aug 2022 19:38
by admin
PeterH wrote: 16 Aug 2022 15:19
admin wrote: 16 Aug 2022 14:19 2) You can only pass literal strings as values, not variables. AFAIK it is like that in PHP. But I'm not sure.
If this is for something like initialization (say in array()) I think it *might* be tolerated.
Thank you for your mercy. ;)

That's why I called array() a special function. It's a pseudo function that offers this particular way to fill an array.

Re: To array definitions

Posted: 16 Aug 2022 21:37
by Filehero

Code: Select all

      The new value-array loop is closer to how it's done in PHP:
      General form value-array:
        foreach($array as $value, [flags]) {
          statement(s) using $value;
        }
Great progress. :appl: :appl:

admin wrote: 16 Aug 2022 19:38 That's why I called array() a special function.
I don't want to over-push, but: with the new looping in mind, the inbuild scripting commands - with many of them returning $Separator-separated item lists (quicksearch(), regexmatches(), folderreport() etc.) - downright "scream" for a glueing handle (another special "constructor" function).



Proposal:

Code: Select all

    + Scripting: Added special function arrayFrom() to create a zero-based indexed array from a "separated" list. Non-existing arrays 
      are created, dimensioned and populated, existing arrays are redimensioned and overwritten.
      
      Syntax
        arrayFrom(inputlist, [separator=" "], [flags])
        
        inputlist: input list with separated items/elements.
        
        separator: [optional] Separates the items in the returned list. Defaults to " ".
        
        flags: [optional] placeholder
      
      
      Notes:
      - You can only pass a string as input, not arrays. The input "list" can be a single word w/o separator.
      - If the input variable does not exist or is an "empty" string (aka "") the returned array has no element (array_count() == 0).
      - The array values won't be quoted by default (see flags).
      - If you like you can append [] to the variable, it makes no difference:
          $a[] = arrayFrom(inputlist, [separator=" "], [flags]);
      - The values are added to the array in the order of the input list, starting with element [0] (see flags).


Ideas for flags:

Code: Select all

        flags:    (in any order)
                        "nd": "no dupes" - every entry of the resulting array is guaranteed to be unique. Once an input item/element already has been added the next input occurrence will be simply skipped.
                        "q": the array elements will be single quoted (q: what if they - by chance - already are?)
                        "r": the array will be build in reversed order.

Re: To array definitions

Posted: 17 Aug 2022 00:22
by eil
That's probably dumb question, but i'm quite "only-with-help-file"-scripter, so i still don't understand- why so many times array stated as global? Isn't all this code part of one script, so after first statement it's global and ought to be same in all sub-scripts?..

Code: Select all

        "_Initialize"
          global $a[];
          $a = "I am Groot!"; //root variable
          $a[0] = "Hi!";
          $a[1] = "Bye!";
        "Say Hi"
          global $a[];
          echo $a[0];
        "Say Bye"
          global $a[];
          echo $a[1];
        "_Terminate"
          global $a[];
          echo $a;

Re: To array definitions

Posted: 17 Aug 2022 01:07
by klownboy
The "Say Hi", "Say Bye", and "_Terminate" are separate scripts in this case. Each script is not indented. If you comment out those 3 global $a[]; statements, you'll see it doesn't work.

Re: To array definitions

Posted: 17 Aug 2022 10:24
by admin
Filehero wrote: 16 Aug 2022 21:37 I don't want to over-push, but: with the new looping in mind, the inbuild scripting commands - with many of them returning $Separator-separated item lists (quicksearch(), regexmatches(), folderreport()
Yes, thanks for the well-crafted proposal. In PHP this function is called explode(): https://www.php.net/manual/de/function.explode.php And, of course, there is also the opposite: implode() (array -> list).

Needs some thinking though... :maf:

Re: To array definitions

Posted: 17 Aug 2022 10:27
by LittleBiG
eil wrote: 17 Aug 2022 00:22 That's probably dumb question, but i'm quite "only-with-help-file"-scripter, so i still don't understand- why so many times array stated as global? Isn't all this code part of one script, so after first statement it's global and ought to be same in all sub-scripts?..

Code: Select all

        "_Initialize"
          global $a[];
          $a = "I am Groot!"; //root variable
          $a[0] = "Hi!";
          $a[1] = "Bye!";
        "Say Hi"
          global $a[];
          echo $a[0];
        "Say Bye"
          global $a[];
          echo $a[1];
        "_Terminate"
          global $a[];
          echo $a;
That is called multi-script in the help file. I can show you another example:

Code: Select all

"Main_script"
 global $a[];
 $a = array("cat","dog");
 echo $a[1]; //dog
 sub "_another_script_hidden" 

"_another_script_hidden"
 global $a[];
 echo $a[1]; //dog
Practically the global means I want to use a global variable. If it doesn't exist, create it. If it does, get it for me .
Actually there could be another approach. When a script calls another one, all global variables could be given through automatically. Unfortunately here it is not the case.

Re: To array definitions

Posted: 17 Aug 2022 10:32
by highend
You don't want to pollute the namespace with global variables...

And if you really want to have a "global" in another script, use perm (and reset it at the end)

Code: Select all

"_Initialize"
    perm $test = "a";
"hello"
    echo $test;
"world"
    echo $test;
"_Terminate"
    unset $test;

Re: To array definitions

Posted: 17 Aug 2022 10:37
by LittleBiG
highend wrote: 17 Aug 2022 10:32 You don't want to pollute the namespace with global variables...

And if you really want to have a "global" in another script, use perm (and reset it at the end)

Code: Select all

"_Initialize"
    perm $test = "a";
"hello"
    echo $test;
"world"
    echo $test;
"_Terminate"
    unset $test;
I am sorry. Was it an anti-global post actually? :lol:

Re: To array definitions

Posted: 17 Aug 2022 12:02
by admin
admin wrote: 17 Aug 2022 10:24
Filehero wrote: 16 Aug 2022 21:37 I don't want to over-push, but: with the new looping in mind, the inbuild scripting commands - with many of them returning $Separator-separated item lists (quicksearch(), regexmatches(), folderreport()
Yes, thanks for the well-crafted proposal. In PHP this function is called explode(): https://www.php.net/manual/de/function.explode.php And, of course, there is also the opposite: implode() (array -> list).

Needs some thinking though... :maf:
In the next beta this will work:

Code: Select all

syntax: count = explode($array, list, [separator="|"])
        count = implode($array, $list, [separator="|"]) 

explode($a, "a,b,c", ","); echo $a[0]; $a[1] = "X"; implode($a, $list); echo $list; //a|X|c
Not in the change log yet. Too lazy, and there will likely be changes. :party:

Re: To array definitions

Posted: 17 Aug 2022 12:54
by LittleBiG
I am just wondering why $key is not set to the index. In this way we would have two ways to get non-associative array elements. One is when the index doesn't matter in processing with foreach and the other when it does.

Code: Select all

 FYI:
        If you do it with a non-associative array the key variable is not set in foreach 
        but remains whatever it is:
          $key="KEY"; $a = array("cat", "dog", "bat"); foreach($a as $key => $value) {echo "$key: $value";};
could give back: 0: cat 1: dog 2: bat

Re: To array definitions

Posted: 17 Aug 2022 12:58
by admin
Yes, I was unsure, but i just tested PHP and they do it too. Next beta...

Re: To array definitions

Posted: 17 Aug 2022 13:30
by Filehero
:tup:

Re: To array definitions

Posted: 17 Aug 2022 19:01
by highend

Code: Select all

Error:  	91 (0x0000005B)
Desc:   	Objektvariable oder With-Blockvariable nicht festgelegt
Dll:    	0
Proc:   	script_Process: foreach

Source: 	XYplorer
XY ver: 	XYplorer 23.50.0019
OS:     	Windows 10 Enterprise, 64-bit, Release 2009, Build 19044.1766
Locale: 	1033 (en-US)
ANSI:   	0, ACP: 1252  (ANSI - Latin I)
Font:   	Segoe UI 8,25, Segoe UI 8,25, DBCS: No
DPI:    	96 (100%), 3840x1600

Date:   	2022-08-17 19:00:02
After upgrading from 0017 to 0019...

Normal foreach loops lead to the above error message^^

Code: Select all

    $actions = <<<>>>
*\Bilder*|#308
D:\Users\%USERNAME%\Kontakte|load "@test.xys";
    >>>;

    foreach($item, $actions, <crlf>, "e") {
        $path = gettoken($item, 1, "|");
    }

Re: To array definitions

Posted: 17 Aug 2022 19:23
by admin
Upload on the way... :)

Re: To array definitions

Posted: 17 Aug 2022 19:34
by highend
Fixed with 0020