Page 1 of 1

Question with Array setup

Posted: 12 Sep 2021 16:02
by SIMMS7400
Hi Folks -

I have a unique situation that I'm wondering how to solve. I have an array and i'm initializing as follows:

Code: Select all

FOR %%a IN (
	"FDR|DLR_DATA_FDRII,DLR_DATA_FDRII|inbox/LOC_DATA_FDRII|REPLACE|STORE_DATA"
    "FDRBS|DLR_DATA_FDRII,DLR_DATA_FDRII|inbox/LOC_DATA_FDRII|REPLACE|STORE_DATA"
    "FDRCS|DLR_DATA_FDRII,DLR_DATA_FDRII|inbox/LOC_DATA_FDRII|REPLACE|STORE_DATA"
) DO FOR /F "tokens=1-4 delims=|" %%A IN (%%a) DO (
        SET "STR[%%A].DLR=%%B"
        SET "STR[%%A].LOC=%%C"
        SET "STR[%%A].IMP=%%D"
        SET "STR[%%A].EXP=%%E"
)
The first part of the array (%%A) is a portion (i.e. a "mask") of a file which is part of a file I will be deriving at a later step. After the array is initialized, I'm going to another function which scans an "Inbox" directory and sets a file to variable called FILENAME. Based on the value of FILENAME, I then need to find the portion of the array that match the file name.

I can dynamically build a check each time I set the FILENAME variable, but where I'm failing is once it hits "FDR", it always stops there because it always renders true. Here is what I tried:

Code: Select all

FOR /L %%A IN (1,1,100) DO (
    CALL SET "MASK=%%FILENAME:~0,%%A%%"
    CALL SET "_CHECK=%%STR[!MASK!].DLR%%"
    
    IF DEFINED _CHECK GOTO BREAK
)
:BREAK
echo %MASK%
echo %_CHECK%
pause
Here are my file names:

Code: Select all

FDR_20210912.csv
FDRBS_20210912.csv
FDRCS_20210912.csv
How do I get around this? Or should I rename the "FDR" file to make it more unique?

I hope I made sense, let me know if you need more info.

Re: Question with Array setup

Posted: 13 Sep 2021 03:05
by T3RRY
SIMMS7400 wrote:
12 Sep 2021 16:02
Hi Folks -

I have a unique situation that I'm wondering how to solve. I have an array and i'm initializing as follows:

Code: Select all

FOR %%a IN (
	"FDR|DLR_DATA_FDRII,DLR_DATA_FDRII|inbox/LOC_DATA_FDRII|REPLACE|STORE_DATA"
    "FDRBS|DLR_DATA_FDRII,DLR_DATA_FDRII|inbox/LOC_DATA_FDRII|REPLACE|STORE_DATA"
    "FDRCS|DLR_DATA_FDRII,DLR_DATA_FDRII|inbox/LOC_DATA_FDRII|REPLACE|STORE_DATA"
) DO FOR /F "tokens=1-4 delims=|" %%A IN (%%a) DO (
        SET "STR[%%A].DLR=%%B"
        SET "STR[%%A].LOC=%%C"
        SET "STR[%%A].IMP=%%D"
        SET "STR[%%A].EXP=%%E"
)
The first part of the array (%%A) is a portion (i.e. a "mask") of a file which is part of a file I will be deriving at a later step. After the array is initialized, I'm going to another function which scans an "Inbox" directory and sets a file to variable called FILENAME. Based on the value of FILENAME, I then need to find the portion of the array that match the file name.

I can dynamically build a check each time I set the FILENAME variable, but where I'm failing is once it hits "FDR", it always stops there because it always renders true. Here is what I tried:

Code: Select all

FOR /L %%A IN (1,1,100) DO (
    CALL SET "MASK=%%FILENAME:~0,%%A%%"
    CALL SET "_CHECK=%%STR[!MASK!].DLR%%"
    
    IF DEFINED _CHECK GOTO BREAK
)
:BREAK
echo %MASK%
echo %_CHECK%
pause
Here are my file names:

Code: Select all

FDR_20210912.csv
FDRBS_20210912.csv
FDRCS_20210912.csv
How do I get around this? Or should I rename the "FDR" file to make it more unique?

I hope I made sense, let me know if you need more info.
If your hardcoding definitions, or using tokens, you don't necessarily need to change conflicting names, simple use a character to flag the end of the string.

That said, powershell is much more efficient with regards to arrays, both in establishing and manipulating them, and has the added benefit of opening up regex -match and -replace options.

Re: Question with Array setup

Posted: 13 Sep 2021 03:24
by SIMMS7400
Thanks, T! Where would I add that character? To the array itself when it initializes?

Re: Question with Array setup

Posted: 13 Sep 2021 06:34
by T3RRY
SIMMS7400 wrote:
13 Sep 2021 03:24
Thanks, T! Where would I add that character? To the array itself when it initializes?
Change the logic a bit in the mask / _Check setion to make use of the variables ] as the terminating character. (Untested):

Code: Select all

    CALL SET "MASK=%%FILENAME:~0,%%A%%]"
    CALL SET "_CHECK=%%STR[!MASK!.DLR%%"
    
    If defined _CHECK  (
      Set ""MASK=!Mask:~0,-1!" %= remove this line if mask var not used again =%
      GOTO BREAK
     )


Re: Question with Array setup

Posted: 13 Sep 2021 06:44
by SIMMS7400
Thanks, T, but that didn't work.

Lets say I have FDRBS_20210912.csv. it's still returning the array definition for the FDR line, not the FDRBS definition.

Re: Question with Array setup

Posted: 13 Sep 2021 07:42
by Squashman
Why wouldn't you use the underscore as a delimiter from the filename to help you match the array?

Re: Question with Array setup

Posted: 13 Sep 2021 07:58
by SIMMS7400
Squash -

Absolutely I can. But there will be situationnwhere I won't have the delimiter to use and will be stuck with this situation that I'm hopping to solve with some additional logic

Re: Question with Array setup

Posted: 13 Sep 2021 08:14
by Squashman
SIMMS7400 wrote:
13 Sep 2021 07:58
Squash -

Absolutely I can. But there will be situationnwhere I won't have the delimiter to use and will be stuck with this situation that I'm hopping to solve with some additional logic
Well you certainly can't do the substring from left to right because that will always match your shorter array variable first that has the same string at the beginning of your longer array variables. So I see your only solution as looping through from right to left and subtracting 1 from the string comparison. It is somewhat inefficient but should be rather quick if you are not going much bigger than 100 for your file names.

Another thing to consider is breaking out of a FOR /L. You have to use special logic for that to happen. It has been discussed several times on the forums. So you will want to look that up. It is not doing what you think it is doing.

Re: Question with Array setup

Posted: 13 Sep 2021 08:40
by SIMMS7400
Thanks, Squash! What do you mean "subtract"?

I'll also review the threads on how to properly break out of a loop.

Re: Question with Array setup

Posted: 13 Sep 2021 08:47
by Squashman
SIMMS7400 wrote:
13 Sep 2021 08:40
What do you mean "subtract"?

Code: Select all

FOR /L %%A IN (10,-1,1) DO......

Re: Question with Array setup

Posted: 13 Sep 2021 09:06
by SIMMS7400
Squash, thank you sir. That seemed to do the trick!!

Now one more question, if perhaps the string on the RIGHT side of the file, like this:
20210913_FDRBS.csv
Is it also possible to search that way too?

Re: Question with Array setup

Posted: 13 Sep 2021 14:26
by Aacini
I think this do what you want:

Code: Select all

set "MASK=%FILENAME%"

:nextChar
set "MASK=%MASK:~0,-1%"
if not defined STR[%MASK%].DLR goto nextChar

echo File: %FILENAME%, MASK: %MASK%
Antonio

Re: Question with Array setup

Posted: 18 Sep 2021 07:57
by SIMMS7400
Thanks Squash and Aacni - both options worked great!