[AMPL 2552] backtick operator and list of built-in parameters

View: New views
4 Messages — Rating Filter:   Alert me  

[AMPL 2552] backtick operator and list of built-in parameters

by Guney Petek :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Greetings,

Is it possible to define a variable including the date and time .run
file is run on? I'd like to name the output file using this stamp
variable.
for example: param stamp symbolic:=`shell "date"`; that is, something
like the backtick operator that do a similar job in BASH...

Secondly, is there a list of all built-in parameters and variables(?)?

thanks //Guney

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To post to this group, send email to ampl@...
To unsubscribe from this group, send email to ampl+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/ampl?hl=en
-~----------~----~----~----~------~----~------~--~---


[AMPL 2553] Re: backtick operator and list of built-in parameters

by Robert Fourer-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


There are builtin functions time and ctime:

   ampl: print time();
   1243702207
   ampl: print ctime();
   Sat May 30 11:50:10 2009
   ampl: print ctime(time());
   Sat May 30 11:50:19 2009

However if you define

   param stamp symbolic = ctime();

the value of stamp won't be updated automatically every time you refer to
it.  You have to do something to cause it to be updated, such as a reset
data:

   ampl: param stamp symbolic = ctime();
   ampl: print stamp;
   Sat May 30 11:52:34 2009
   ampl: print stamp;
   Sat May 30 11:52:34 2009
   ampl: print stamp;
   Sat May 30 11:52:34 2009
   ampl: reset data stamp;
   ampl: print stamp;
   Sat May 30 11:53:09 2009

There are lists of all the functions in the appendix to the AMPL book, but
there's no master list of builtin parameters.

Bob Fourer
4er@...


> -----Original Message-----
> From: ampl@... [mailto:ampl@...]
> On Behalf Of Guney Petek [guneyp@...]
> Sent: Friday, May 29, 2009 7:15 PM
> To: AMPL Modeling Language
> Subject: [AMPL 2552] backtick operator and list of built-in parameters
>
>
> Greetings,
>
> Is it possible to define a variable including the date and time .run
> file is run on? I'd like to name the output file using this stamp
> variable.
> for example: param stamp symbolic:=`shell "date"`; that is, something
> like the backtick operator that do a similar job in BASH...
>
> Secondly, is there a list of all built-in parameters and variables(?)?
>
> thanks //Guney
>



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To post to this group, send email to ampl@...
To unsubscribe from this group, send email to ampl+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/ampl?hl=en
-~----------~----~----~----~------~----~------~--~---


[AMPL 2569] Re: backtick operator and list of built-in parameters

by Guney Petek :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On May 30, 12:58 pm, "Robert Fourer" <4...@...> wrote:
> There are builtin functions time and ctime:
>

Thank you for your answer. I used ctime() for that specific case. But
I often feel the need to pass output of a shell command into an AMPL
parameter. Is that possible?

I am aware that environment variables set in shell before AMPL is
invoked can be reached within AMPL. Is it possible to do that during
AMPL runtime, as well?

I thought I had sent this e-mail earlier. I am sorry if you have
received it twice.

thanks, \\Guney

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To post to this group, send email to ampl@...
To unsubscribe from this group, send email to ampl+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/ampl?hl=en
-~----------~----~----~----~------~----~------~--~---


[AMPL 2571] Re: backtick operator and list of built-in parameters

by Matthias-41 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


> I often feel the need to pass output of a shell command into an AMPL
> parameter. Is that possible?

I sometimes do this by redirecting the output of the shell command to
a file, then reading the file into an ampl parameter. e.g., this can
check whether a certain file already exists:

option my_file_name "some file.txt";
option status_file "_stat_file.txt";
param readfile symbolic;

shell ('if test -f "' & $my_file_name & '" ; then echo 1 ; else echo
0 ; fi') > ($status_file);
close ($status_file);
read readfile < ($status_file);
close ($status_file);
remove ($status_file);

Note that this will only work if the shell command produces a single
word of output. If you need a longer string, e.g., the result of the
"uptime" command, you could enclose it in quotes, like this:

shell ('echo \"`uptime`\"') > ($status_file);
close ($status_file);
read readfile < ($status_file);
close ($status_file);
remove ($status_file);

I haven't figured out a way to do this without creating a temporary
file. But if you just need a status flag back from the shell command,
you can use the built-in shell_exitcode parameter. e.g., an example
from Robert Fourer of a better way to check for a file's existence:

shell ('test -f "' & $my_file_name & '"'); # or use "file -f ..." or
"dir ..."
if shell_exitcode == 0 then {
  # do something with the file
} else {
  # the file is not there -- do something else
}

For some reason, if the file doesn't exist ampl reports "exit code 1"
but shell_exitcode is set to 256. But the example above is still
workable.

Matthias Fripp


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To post to this group, send email to ampl@...
To unsubscribe from this group, send email to ampl+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/ampl?hl=en
-~----------~----~----~----~------~----~------~--~---