StrMatch issue - load ticker name from csv

Hello to everyone.
StrMatch() behaves in a way I don't understand when I try to compare a ticker loaded from a CSV file with the string from Name().

Here's the code:

// function that loads and parses the setup file	
function LoadSetupCSV( sourcePath, fileName )
{
    ans = 0;
    _N( list = fdir( sourcePath + "*.*", 1 ) );

    for( i = 0; ( filename = StrExtract( list, i ) ) != ""; i++ )
    {
        filename;

        if( StrMatch( filename, fileName ) )
        { 
            ans = 1;
            break;
        }
    }

    if( ans == 0 )
    {
        PopupWindow( "File not found", "SOURCE ERROR" );
        _exit();
    }

    fh = fopen( sourcePath + fileName, "r" );

    if( fh )
    {
        ticker = "";
        IS_start = 0;
        lineNum = 0;

        while( ! feof( fh ) )
        {
            line = fgets( fh ); 
            switch( lineNum )
            {
                case 1:
                {
                    ticker = StrExtract( line, 2 );
                }

                case 2:
                {
                    IS_start = StrToNum( StrExtract( line, 2 ) );
                }
            }
            lineNum++;
        }
        fclose( fh );
        
        VarSetText("ticker", ticker);
        VarSet("IS_start", IS_start);
    }
    else
    {
        PopupWindow( "file can not be opened", "FILE Error" );
        _exit();
    }
}

//	the script
sourcePath = "<write here the path folder where the file 'text.csv' has been saved>";
fileName = "test.csv";	
LoadSetupCSV( sourcePath, fileName );
loadedTicker = VarGetText("ticker");
loadedISstart = VarGet("IS_start");;

ticker = Name();
if(StrMatch(ticker, loadedTicker) == 1)
{
	PopupWindow("Tickers match", "OK");
}
else
{
	PopupWindow("Tickers do not match\n" + ticker + " != " + loadedTicker, "ERROR");
}

You could find the file "test.csv" here : test.csv

once the file has been downloaded and the sourcePath variable modified (indicating the location where the file has been saved), If I try to run the script then the ticker check fails regardless of whether the ticker has been correctly selected or not.

Look at this:
Schermata 2024-03-29 alle 18.26.55

Why doesn't StrMatch() return true when those strings are matching?

My guess is that there is probably a new line character in the csv file, at the end of the ticker field, that is included in the ticker string. You should strip that out before comparing.

1 Like

@2DD, I think @PeterD has nailed the reason for your problem perfectly: use the StrTrim() function to remove unwanted (non-visible) characters!

Thank you Peter, I'll check this out

Thank you Beppe for the hint of using StrTrim() function. Never used before :slight_smile: