Author Topic: ForEach Loop Not Working As Expected In V16  (Read 94 times)

total_annihilation00

  • Power Member
  • ****
  • Posts: 173
  • Tech Savant\ Envisioneering
    • View Profile
ForEach Loop Not Working As Expected In V16
« on: Today at 07:12:36 »
it mentions ForEach has been added to MC but the ChatGPT AI scripts which incorporated ForEach Loops (for my "Smart Open (Ctrl+F9)" script all failed —worked when I used plain For Loop: Changelog states: "arrays and collections. (e.g. foreach $item in $array { ... }) MultiScript "

Here's my full script that was reduced to just 55 lines of MultiScript code from 443 initially then 140 /w Boolean OR's but now it's optimized greatly & highly scalable. But this is broken because ForEach Loop is not working properly:
Code: [Select]
@var $path = GetSourceFocusPath();
@var $ext = StrToLower( PathGetFileExtPart( $path, 2 ) );

@var $rules[] = {'udc|txt,ini,log,ahk,mtxt,vbs,conf,cpp,h,rc,asm,nfo,info,ps1,xml,jsee,cfg|b85b4bf468ab418e87b4e09e95dca4a0','udc|jpg,jpeg,jpe,jfif,png,gif,webp,bmp,tiff,psd,svg|c3424b15b3dd4a75a83ce0d9ba857bbb','udc|pdf,epub,djvu,mobi,fb2,cb7,cbr,cbt,cbz,prc,azw|3bd046dd55484d62aa82285076ea1c15','udc|zip|e271faf8469f4e96ac335fde319ab818','udc|chm|45cd3dfb7fc348ec99de80122481c55d','udc|sms,gen,smd,bin,md|468167006ee54bb995e28940823ec958','udc|sfc,smc,fig|6d1061dff5014030bcd2062fee6701cf','udc|nes|3d966becbbb640f983aab1f18a109f51','udc|wav,mp3,mid,midi,wma,flac,ogg,aac,alac,aiff|4e1f3a7ca0224c6ba0ff02f3f07cd7c4','udc|cur,ani|3c16c3020c384373a4dd96060cb856e3','run|pptx|\"C:\\Program Files\\Microsoft Office\\root\\Office16\\POWERPNT.EXE\"|/S \"{$path}\"','run|ppt|\"C:\\Program Files\\Microsoft Office\\root\\Office16\\POWERPNT.EXE\"|\"{$path}\"','run|sln|\"D:\\Download\\VS 19 CE\\Common7\\IDE\\devenv.exe\"|\"{$path}\"','udc|exe|e07317b5406b4c19b069ea4bd926f706'};

@var $matched = 0;

foreach $rule in $rules
{
    @var $parts = StrTokenize2Array( $rule, "|" );
    @var $kind = $parts[0];
    @var $extList = StrTokenize2Array( $parts[1], "," );

    if( arrayIFind( $extList, $ext ) > -1 )
    {
        $matched = 1;

        if( $kind == "udc" )
        {
            MC.RunUserCmd ID=$parts[2];
        }
        else if( $kind == "run" )
        {
            MC.Run CMD=$parts[2] ARG=$parts[3];
        }

        break;
    }
}

if( $matched == 0 )
{
    MessageBox( "SmartOpen", "No handler defined for extension: ." + $ext, 0 );
}

I get the following errors:
2026-05-06 10:33:33.325 Script engine error => Internal Script Function - Parse Error : "in"
2026-05-06 10:33:33.325 Script engine error - Line : 22, Error : -1 => Code : "foreach $rule in $rules"

When I resorted to this For Loop version /w Arrays, it worked perfectly w/o any errors:
Code: [Select]
@var $path = GetSourceFocusPath();
@var $ext = StrToLower( PathGetFileExtPart( $path, 2 ) );

@var $rules[14];

// ====================== TEXT FILES ======================
$rules[0]  = 'udc|txt,ini,log,ahk,mtxt,vbs,conf,cpp,h,rc,asm,nfo,info,ps1,xml,jsee,cfg|b85b4bf468ab418e87b4e09e95dca4a0';
// ====================== IMAGE FILES ======================
$rules[1]  = 'udc|jpg,jpeg,jpe,jfif,png,gif,webp,bmp,tiff,psd,svg|c3424b15b3dd4a75a83ce0d9ba857bbb';
// ====================== DOCUMENT FILES ======================
$rules[2]  = 'udc|pdf,epub,djvu,mobi,fb2,cb7,cbr,cbt,cbz,prc,azw|3bd046dd55484d62aa82285076ea1c15';
$rules[3]  = 'udc|chm|45cd3dfb7fc348ec99de80122481c55d';
// ====================== ARCHIVE ======================
$rules[4]  = 'udc|zip|e271faf8469f4e96ac335fde319ab818';
// ====================== EMULATORS ======================
$rules[5]  = 'udc|sms,gen,smd,bin,md|468167006ee54bb995e28940823ec958';
$rules[6]  = 'udc|sfc,smc,fig|6d1061dff5014030bcd2062fee6701cf';
$rules[7]  = 'udc|nes|3d966becbbb640f983aab1f18a109f51';
// ====================== AUDIO ======================
$rules[8]  = 'udc|wav,mp3,mid,midi,wma,flac,ogg,aac,alac,aiff|4e1f3a7ca0224c6ba0ff02f3f07cd7c4';
// ====================== CURSORS ======================
$rules[9]  = 'udc|cur,ani|3c16c3020c384373a4dd96060cb856e3';
// ====================== MICROSOFT OFFICE ======================
$rules[10] = 'run|pptx|C:\\Program Files\\Microsoft Office\\root\\Office16\\POWERPNT.EXE|/S "{$path}"';
$rules[11] = 'run|ppt|C:\\Program Files\\Microsoft Office\\root\\Office16\\POWERPNT.EXE|"{$path}"';
$rules[12] = 'run|sln|D:\\Download\\VS 19 CE\\Common7\\IDE\\devenv.exe|"{$path}"';
// ====================== DOSBox ======================
$rules[13] = 'udc|exe|e07317b5406b4c19b069ea4bd926f706';

@var $matched = 0;
@var $i;
@var $rule;
@var $parts;
@var $kind;
@var $extList;

for( $i = 0; $i < 14; $i++ )
{
    $rule = $rules[$i];
    $parts = StrTokenize2Array( $rule, "|" );
    $kind = $parts[0];
    $extList = StrTokenize2Array( $parts[1], "," );

    if( arrayIFind( $extList, $ext ) > -1 )
    {
        $matched = 1;

        if( $kind == "udc" )
        {
            MC.RunUserCmd ID={$parts[2]};
        }
        else if( $kind == "run" )
        {
            MC.Run CMD="{$parts[2]}" ARG='{$parts[3]}';
        }

        break;
    }
}

if( $matched == 0 )
{
    MessageBox( "SmartOpen", "No handler defined for extension: ." + $ext, 0 );
}

Also the documentation needs to be updated so the AI can glean the syntax from the website. This is what I assume was added:
MultiScript supports the ternary operator (e.g. $a = $b ? $c : $d) MultiScript supports a "foreach" loop for iterating through
Quote
arrays and collections. (e.g. foreach $item in $array { ... }) MultiScript
supports new functions: Pow, abs, clamp, IsNumeric, StrStartsWith, StrEndsWith,
StrContains, StrFormat, StrPadLeft, StrPadRight, StrCount, StrRepeat,
ArrayReverse, ArraySlice MultiScript supports boolean operators AND / OR / NOT.
« Last Edit: Today at 13:20:49 by total_annihilation00 »
~The World's Deceit Has Raped My Soul, We Melt The Plastic People Down Then We Melt Their Plastic Town~


Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4886
    • View Profile
    • Multi Commander
Re: ForEach Loop Not Working As Expected In V16
« Reply #1 on: Today at 07:28:49 »
Those new function are in BETA only.. so documentation can't be uptodate yet since it is not released yet-

You missed the parantheses

Code: [Select]
foreach( $rule in $rules)

Not
Code: [Select]
foreach $rule in $rules


MCScript has paratheses around all experssions for / if / for / while
« Last Edit: Today at 07:35:43 by Mathias (Author) »

total_annihilation00

  • Power Member
  • ****
  • Posts: 173
  • Tech Savant\ Envisioneering
    • View Profile
Re: ForEach Loop Not Working As Expected In V16
« Reply #2 on: Today at 07:54:18 »
Thank you so much for reviving my script, Mathias ! Sorry to trouble you, but can you tell me why this particular script is failing (I'm guessing it's the ArrayFind)?
I get the following errors:
2026-05-06 11:22:54.288 Script engine error => Failed to process token - "$rules"
2026-05-06 11:22:54.288 Script engine error => Failed to initialize variable "$rules" with "
2026-05-06 11:22:54.288 Script engine error => Failed to define variable - "@var $rules[] ="
2026-05-06 11:22:54.288 Script engine error - Line : 11, Error : -1 => Code : "@var $rules[] ="

Here's the full code:
Code: [Select]
@var $path = GetSourceFocusPath();
@var $ext = StrToLower( PathGetFileExtPart( $path, 2 ) );

@var $textExts[] = {'txt','ini','log','ahk','mtxt','vbs','conf','cpp','h','rc','asm','nfo','info','ps1','xml','jsee','cfg'};
@var $imageExts[] = {'jpg','jpeg','jpe','jfif','png','gif','webp','bmp','tiff','psd','svg'};
@var $docExts[] = {'pdf','epub','djvu','mobi','fb2','cb7','cbr','cbt','cbz','prc','azw'};
@var $kegaExts[] = {'sms','gen','smd','bin','md'};
@var $snesExts[] = {'sfc','smc','fig'};
@var $audioExts[] = {'wav','mp3','mid','midi','wma','flac','ogg','aac','alac','aiff'};
@var $cursorExts[] = {'cur','ani'};

@var $rules[] =
{
    {'udc', $textExts, 'b85b4bf468ab418e87b4e09e95dca4a0'},
    {'udc', $imageExts, 'c3424b15b3dd4a75a83ce0d9ba857bbb'},
    {'udc', $docExts, '3bd046dd55484d62aa82285076ea1c15'},
    {'udc', {'zip'}, 'e271faf8469f4e96ac335fde319ab818'},
    {'udc', {'chm'}, '45cd3dfb7fc348ec99de80122481c55d'},
    {'udc', $kegaExts, '468167006ee54bb995e28940823ec958'},
    {'udc', $snesExts, '6d1061dff5014030bcd2062fee6701cf'},
    {'udc', {'nes'}, '3d966becbbb640f983aab1f18a109f51'},
    {'udc', $audioExts, '4e1f3a7ca0224c6ba0ff02f3f07cd7c4'},
    {'udc', $cursorExts, '3c16c3020c384373a4dd96060cb856e3'},

    {'run', {'pptx'}, '"C:\\Program Files\\Microsoft Office\\root\\Office16\\POWERPNT.EXE"', '/S "{$path}"'},
    {'run', {'ppt'},  '"C:\\Program Files\\Microsoft Office\\root\\Office16\\POWERPNT.EXE"', '"{$path}"'},
    {'run', {'sln'},  '"D:\\Download\\VS 19 CE\\Common7\\IDE\\devenv.exe"', '"{$path}"'},

    {'udc', {'exe'}, 'e07317b5406b4c19b069ea4bd926f706'}
};

@var $matched = 0;

foreach( $rule in $rules)
{
    if( arrayIFind( $rule[1], $ext ) > -1 )
    {
        $matched = 1;

        if( $rule[0] == 'udc' )
        {
            MC.RunUserCmd ID=$rule[2];
        }
        else if( $rule[0] == 'run' )
        {
            MC.Run CMD=$rule[2] ARG=$rule[3];
        }

        break;
    }
}

if( $matched == 0 )
{
    MessageBox( "SmartOpen", "No handler defined for extension: ." + $ext, 0 );
}

P.S: Nevermind, the arrays definition is probably the issue, I'm using my old code… P.P.S: Nevermind I figured it out, switched from For Loop to ForEach Loop. changed the For part "for( $i = 0; $i < 14; $i++ )" to "foreach( $rule in $rules)" and commented out "$rule = $rules[$i];" and also commented out both "@var $i;" and "@var $rule;" !
Quote
foreach may be slightly faster in theory because it bypasses manual index increment and bounds checking, but the gain is microseconds at most.
« Last Edit: Today at 13:31:19 by total_annihilation00 »
~The World's Deceit Has Raped My Soul, We Melt The Plastic People Down Then We Melt Their Plastic Town~