Page 1 of 1

Question about ord function and ASCII

Posted: Mon Mar 01, 2021 7:17 am
by vistro
Hi Folks,
I tried to use $ifThen ($ifThenR) and ord functions to differentiate the scenarios containing 's'.
So I would expect the set fest to only have one element, b. When I run the following code, fest always has two elements a, b.
Please help me figure out what went wrong.

Code: Select all

$set Scenarios static
$ifThen (ord('%Scenarios%',1)==ord('s',1))
$set ff '*'
$else
$set ff ''
$endif
;
set fest /
%ff% a
     b
/;
display fest;
[code]

Best/JG

Re: Question about ord function and ASCII

Posted: Tue Mar 02, 2021 8:59 am
by bussieck
You did a ifThen comparing the left and right string (they are obviously different) and hence you always fall into the else part. You probably want to do a ifThenE, but ord() is not part of the functions available at compile time (see https://www.gams.com/latest/docs/UG_Dol ... 2__25_eval). So no joy. If you insist on the "starting with s" check you will need to resort to some external feature, e.g. grep:

Code: Select all

$set Scenarios static
$call echo %Scenarios% | grep -q "^s"
$ifThen errorlevel 0
$set ff '*'
$else
$set ff ''
$endif
set fest /
%ff% a
     b
/;
display fest;
If you form the name of you scenario like a filename and take the first (or last part) as the indicator to include a or not you can do this entirely in GAMS usiing the the setNames command (https://www.gams.com/latest/docs/UG_Dol ... ARsetnames)

Code: Select all

$set Scenarios s.static
$setNames %Scenarios% * type sname
$ifThen %type%==s
$set ff '*'
$else
$set ff ''
$endif
set fest /
%ff% a
     b
/;
display fest;
-Michael

Re: Question about ord function and ASCII

Posted: Tue Mar 02, 2021 11:00 pm
by vistro
Thanks, Michael. Both work just fine. :D