String List, Check for Existence of Search String

String List, Check for Existence of Search String
Checks for the existence of string elements in the list that match the search string.
Configs
  • C1:Set multiline type list Data.#{EL}
  • C2:Set CSV type list data.#{EL}
  • C3:Set search strings. *#{EL}
  • C4:Case-sensitive search of alphabetic characters
  • C5:Select STRING type item for search result(‘true’ ‘false’).
  • C6:Select NUMERIC type item for number of search result.
  • C7:Select STRING type item for element indexes of search result.
Script (click to open)


main();

function main(){

    const multilineList = configs.get('conf_multilineList');
    const csvList = configs.get('conf_csvList');
    const keyWord = configs.get('conf_keyWord');
    const caseSensitive = configs.get('conf_caseSensitive');
    const conf_searchResultBoolean = configs.get('conf_searchResultBoolean');
    const conf_searchResultNum = configs.get('conf_searchResultNum');
    const conf_searchResultElementIndexes = configs.get('conf_searchResultElementIndexes');

    if(multilineList === '' && csvList === ''){
        throw 'Either Multiline List or CSV List is required.';
    }

    //Search Type
    let  searchType = caseSensitive +'%';

    //検索結果の件数、行インデックスのいずれかが求められている場合は全件検索
    if(conf_searchResultNum !== '' || conf_searchResultElementIndexes !== ''){
        searchType += 'true';
    }else{
        searchType += 'false';
    }

    let searchResult;
    if(multilineList !== ''){
        engine.log('Multiline List <- key word: ' + keyWord);
        searchResult = search(multilineList, keyWord, '\n', searchType);
    }else{
        engine.log('CSV List <- key word: ' + keyWord);
        searchResult = search(csvList, keyWord, ',', searchType);
    }

    engine.log('Search Result: ' + JSON.stringify(searchResult));

    if(conf_searchResultBoolean !== ''){
        engine.setDataByNumber(conf_searchResultBoolean, searchResult.num > 0 ? 'true' : 'false');
    }

    if(conf_searchResultNum !== ''){
        engine.setDataByNumber(conf_searchResultNum, new java.math.BigDecimal(searchResult.num));
    }

    if(conf_searchResultElementIndexes !== ''){
        engine.setDataByNumber(conf_searchResultElementIndexes, searchResult.elementIndexes);
    }

}

/*
@param list {string} String List
@param keyWord {string} Search String
@param delimiter {string}
@param searchType {string} Case Sensitive (true or false)%All Search(true or false)
@return result {JSON Object {"num","elementIndexes"}}
*/
function search(list, keyWord, delimiter, searchType){

    const searchTypeArr = searchType.split('%');
    const sensitiveFlag = searchTypeArr[0] === 'true' ? true : false ;
    const searchAllFlag = searchTypeArr[1] === 'true' ? true : false ;

    const result = {};
    let num = 0;
    let elementIndexes = '';

    const array = list.split(delimiter);
    for( let i = 0 ; i < array.length ; i++ ){

        //要素と検索文字列の比較で大文字小文字を区別する場合
        if((sensitiveFlag && array[i] === keyWord) ||
           //要素と検索文字列の比較で大文字小文字を区別しない場合
           (! sensitiveFlag && array[i].toUpperCase() === keyWord.toUpperCase())
        ){
            num += 1;
            elementIndexes += String(i) + ',';
        }

        //searchAllFlag === false の場合、検索文字列と一致する要素が見つかった時点で検索を終了
        if(num > 0 && ! searchAllFlag){
            result.num = num;
            result.elementIndexes = elementIndexes.slice(0,-1);
            return result;
        }

    }

    result.num = num;
    result.elementIndexes = elementIndexes.slice(0,-1);

    return result;
}

/*
- リストデータは要素と要素を改行またはコンマで区切られていることが想定されています。
- リストデータについて、複数行、CSVの両方が入力されている場合、複数行形式のものが検索対象になります。
- 先頭要素のインデックスはゼロです。検索結果の件数が2以上の場合、要素インデックスはカンマ区切りで入力されます。
- C6, C7 の設定が省略されている場合、検索は検索文字列と一致する要素が見つかった時点で終了されます。
    - 検索文字列と一致する要素の "存在のみ" を確認したい場合、C6,C7 設定の省略が推奨されます。
- The list data is expected to be separated from elements by line breaks or commas.
- If both multi-line and CSV are entered for the list data, the multi-line type is the target of the search.
- The index of the first element is zero. If the number of search results is 2 or more, the element indexes are entered comma-separated.
- If the C6 and C7 settings are skipped, the search terminates when an element matching the search string is found.
    - If you want to check "existence only" of elements matching the search string, omitting C6 and C7 settings is recommended.
*/

Download

2022-11-16 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/string-list-check-existence-of-search-string/

Notes

  • The list data is expected to be separated from elements by line breaks or commas.
  • If both multi-line and CSV are entered for the list data, the multi-line type is the target of the search.
  • The index of the first element is zero. If the number of search results is 2 or more, the element indexes are entered comma-separated.
  • If the C6 and C7 settings are skipped, the search terminates when an element matching the search string is found.
    • If you want to check “existence only” of elements matching the search string, omitting C6 and C7 settings is recommended.

Capture

See also

Scroll to Top

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading