Associative array call with a math expression inside doublequotes vs singlequotes

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Brano
Posts: 38
Joined: 08 Jul 2024 11:52

Associative array call with a math expression inside doublequotes vs singlequotes

Post by Brano »

Single quotes behave differently than double quotes. When calling an array and using a math operator inside doublequotes, the operator behaves as an operator, even when there are strings around it. When using single quotes, the operator is a string.

Code: Select all

 $a[1+2] = 10;
 $a["1+2"] = 20; 
 $x = $a["1+2"]; //10
 $y = $a['1+2']; //20
The problem I have is I want to create an associative array with folders' names as the keys. The folder names contain a "-" character. I am using this in a scripted column:

Code: Select all

 set $subFoldersNum, foldersize(, "<d>", 0);
 set $folderList, listfolder(,,2+4);
 $counter = $subFoldersNum;
 foreach($i, $folderList) {
  $a[$i] = $counter;
  $counter = $counter - 1;
 } 
 return $a[<cc_name>];
It seems the doublequotes are used internally by default because it does not work as intended.

Image

What could be a solution?

Thanks.

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

Re: Associative array call with a math expression inside doublequotes vs singlequotes

Post by admin »

1) Interesting observation. I completed the test field like this:

Code: Select all

// array test
  $a[3] = 1;
  $a[1+2] = 2;
  $a["1+2"] = 3; 
  $a['1+2'] = 4; 
  echo $a[3]; //2
  echo $a[1+2]; //2
  echo $a["1+2"]; //2
  echo $a['1+2']; //4
I agree that the 3rd case $a["1+2"] = 3; is a bit surprising and could even be considered a bug. But it can't be changed without breaking old user code, it's just something you have to know if you use mathematical terms as assoc array indices (which is a bit crazy in itself).

2) Still there is something to fix. The next beta will work better. Nope, the fix has to be in your code: return $a[quote(<cc_name>)];

3) Looks like you want to serial number the subfolders in a folder. Why?

Brano
Posts: 38
Joined: 08 Jul 2024 11:52

Re: Associative array call with a math expression inside doublequotes vs singlequotes

Post by Brano »

I'd like to implement special sorting:

Always sort files descending by date modified AND Be able to swap sorting of folders alphabetically either descending or ascending.

Image Image

So I am using two custom columns #SortA and #SortB, mixed type, format date (and date columns are globally set to be descending by default). Taking the date for files and taking a scriped column for folders.
There are two hidden scripted columns, for each sort one. And one scriped colums assigns numbers to the folders ascending and the other one descending.

So I just switch between #SortA and #SortB. The files stay and the folders are switched.

The problem comes when there are numbers around dashes in the folder name:
Image

I come from Lua and its tables, this would be basically done the same way in Lua, with associative array/table. I guess it could be done another way but this seems to me the fastest to code. Associative array is a nice thing but at the moment we are not able to use filenames as keys and considering this is a file manager it is not nice. Could it be fixed somehow...?

Or could you suggest another way how to make this done? I am new to xyplorer.

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

Re: Associative array call with a math expression inside doublequotes vs singlequotes

Post by admin »

Okay. A lot of work.

Change the last line of your script to this and the problem goes away:

Code: Select all

return $a[quote(<cc_name>)];

Brano
Posts: 38
Joined: 08 Jul 2024 11:52

Re: Associative array call with a math expression inside doublequotes vs singlequotes

Post by Brano »

That's it! Thanks.

Brano
Posts: 38
Joined: 08 Jul 2024 11:52

Re: Associative array call with a math expression inside doublequotes vs singlequotes

Post by Brano »

But it's not very effective. With 300 folders it takes several seconds.

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

Re: Associative array call with a math expression inside doublequotes vs singlequotes

Post by admin »

If I understand you correctly, all that's left is to keep the folders always on top and in descending alphabetical order, right?

Brano
Posts: 38
Joined: 08 Jul 2024 11:52

Re: Associative array call with a math expression inside doublequotes vs singlequotes

Post by Brano »

Well, the options would need to be:

Sort folders always alphabetically ascending.
Sort folders always alphabetically descending.
Both above turned off = sort like files.

And these options would need to be on the folder level, not global, so they can be saved in Folder View Settings.

Keep folders on top for me is fine global. But if these above options were done on a folder level, this one could be too.

A greater FR would be to specify a different sorting order for folders and a different one for files.

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

Re: Associative array call with a math expression inside doublequotes vs singlequotes

Post by admin »

Okay, I was just asking. ATM I find it a bit too exotic to be useful for an audience > 1. :)

Brano
Posts: 38
Joined: 08 Jul 2024 11:52

Re: Associative array call with a math expression inside doublequotes vs singlequotes

Post by Brano »

And a question - is there a way to obtain the "Index" column property in a scripted column script? In my script I could just take this and subtract it from the subfolder count and would not have to loop through alll the subfolders.

Image

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

Re: Associative array call with a math expression inside doublequotes vs singlequotes

Post by admin »

Folder and files can mix if you sort by Index. But I think it does not matter for your algo. And is it really necessary to subtract the count? Why not just sort by Index?

But how to retrieve the Index? Hmmm ... highend?

highend
Posts: 14925
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Associative array call with a math expression inside doublequotes vs singlequotes

Post by highend »

@Don: yeah?

Atm only by using listpane() / report() and gettokenindex() but this isn't at least super fast...

An inbuilt <cc_index> would be nice (could use this as well)...
One of my scripts helped you out? Please donate via Paypal

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

Re: Associative array call with a math expression inside doublequotes vs singlequotes

Post by admin »

highend wrote: 12 Jul 2024 14:08 An inbuilt <cc_index> would be nice (could use this as well)...
For what (roughly)?

Brano
Posts: 38
Joined: 08 Jul 2024 11:52

Re: Associative array call with a math expression inside doublequotes vs singlequotes

Post by Brano »

In one case I want to sort them ascending and in the other descending. (I've noticed sorting by Index does not obey "Keep folders on top"). And I want to sort files separately.

Image Image
Last edited by Brano on 12 Jul 2024 14:15, edited 1 time in total.

highend
Posts: 14925
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Associative array call with a math expression inside doublequotes vs singlequotes

Post by highend »

Exact same reason as the op, custom sort ccs.

But this is only 2, so maybe not enough to implement this xD
One of my scripts helped you out? Please donate via Paypal

Post Reply