Question about ord function and ASCII

Problems with syntax of GAMS
Post Reply
vistro
User
User
Posts: 6
Joined: 4 years ago

Question about ord function and ASCII

Post 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
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Question about ord function and ASCII

Post 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
vistro
User
User
Posts: 6
Joined: 4 years ago

Re: Question about ord function and ASCII

Post by vistro »

Thanks, Michael. Both work just fine. :D
Post Reply