Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Quoting arguments to a user defined function

May
120
1
I'm having trouble with levels of quoting. What I'm trying to do is
write a function which returns the output of the Gnu date command.
Annoyingly, the command takes a format string parameter which makes
heavy use of % characters. So I need to quote the argument:


>C:\Utils\Gnuwin32\bin\date +`%d/%m/%Y`
05/07/2008

If I try to make this into a user defined function:

function fmtdate=`%@EXECSTR[C:\Utils\Gnuwin32\date +^`%$^`]`

it does not work as expected:


>function
fmtdate=%@EXECSTR[C:\Utils\Gnuwin32\bin\date +`%$`]


>echo %@FMTDATE[`%D%M%Y`]
ECHO is OFF

I don't know of a good way to debug this, but it looks to me like the
quoting is wrong somehow. Can anybody offer me any suggestions (either
on how to see what's going on, or on how to fix the quoting up so that
I can call the function in a reasonably natural way)?

(Actually, if someone can point me to a built in function that will
allow me to format the current date *and time* in an arbitrary way,
rather than just the fixed formats described in "Date Display Formats"
that would do me fine, as well!)

Paul.
 
p.f.moore wrote:

> I'm having trouble with levels of quoting. What I'm trying to do is
> write a function which returns the output of the Gnu date command.
> Annoyingly, the command takes a format string parameter which makes
> heavy use of % characters. So I need to quote the argument:
>
> Quote:
> >C:\Utils\Gnuwin32\bin\date +`%d/%m/%Y`
>
> 05/07/2008
>
> If I try to make this into a user defined function:
>
> function fmtdate=`%@EXECSTR[C:\Utils\Gnuwin32\date +^`%$^`]`
>
> it does not work as expected:
>
>
> Quote:
> >function
>
> fmtdate=%@EXECSTR[C:\Utils\Gnuwin32\bin\date +`%$`]
>
>
> Quote:
> >echo %@FMTDATE[`%D%M%Y`]
>
> ECHO is OFF
>
> I don't know of a good way to debug this, but it looks to me like the
> quoting is wrong somehow. Can anybody offer me any suggestions (either
> on how to see what's going on, or on how to fix the quoting up so that
> I can call the function in a reasonably natural way)?

The back quotes are processed and removed in the @fmtdate function; they
aren't passed through to be processed by the next function or command.

The easiest workaround would be to use SETDOS /X to prevent processing
of the % characters in your function (i.e., SETDOS /X-3 to turn it off
and SETDOS /X+3 to reenable it).

Rex Conn
JP Software
 
p.f.moore wrote:
| (Actually, if someone can point me to a built in function that will
| allow me to format the current date *and time* in an arbitrary way,
| rather than just the fixed formats described in "Date Display Formats"
| that would do me fine, as well!)

Look at the topic "variablecats.htm" - Variables by categories - in the
section on date and time variables:

_year _month _day _hour _minute _second

which give you each field (also _monthf, _imonth, _imonthf if month names
are relevant). From these you can define your own function(s) to combine
them in any manner you want. I have no doubt the UDF will be executed faster
than calling an external program, and capturing its output via @execstr[].

WARNING! When you execute any such function just before a higher unit
changes (e.g., near the hour), it is possible that you would get
inconsistency - some fields retrieved before the change, some after the
change. For example, if you are near midnight on New Year's eve, and you
retrieve time elements before, date elements after midnight, you can get
2009-01-01 23:59:59. A safer method is to use one of the variables _datetime
/ _isodatetime / _utcdatetime and use @instr to locate the subfields from
the yyyyMMddhhmmss format.
--
HTH, Steve
 
2008/7/5 Steve Fábián <>:

> p.f.moore wrote:
> | (Actually, if someone can point me to a built in function that will
> | allow me to format the current date *and time* in an arbitrary way,
> | rather than just the fixed formats described in "Date Display Formats"
> | that would do me fine, as well!)
>
> Look at the topic "variablecats.htm" - Variables by categories - in the
> section on date and time variables:
>
> _year _month _day _hour _minute _second
>
> which give you each field (also _monthf, _imonth, _imonthf if month names
> are relevant).

Yes, I was aware of these. Generally, I find messing round with manual
formatting is a real nuisance, which is why I prefer approaches which
let me use format codes. (For example, the month as I want it is
%@FORMAT[02,%_MONTH] to get leading zeroes - add a few of those
together and you very rapidly exceed any sane line length.

I could use ^ for line continuations, but the intent of the code still
gets obscured by the formatting...

Paul.
 
2008/7/5 rconn <>:

> The back quotes are processed and removed in the @fmtdate function; they
> aren't passed through to be processed by the next function or command.
>
> The easiest workaround would be to use SETDOS /X to prevent processing
> of the % characters in your function (i.e., SETDOS /X-3 to turn it off
> and SETDOS /X+3 to reenable it).

Aha! Something like

function fmtdate=`%@EXECSTR[setdos /X-3 & C:\Utils\Gnuwin32\bin\date
+%$ & setdos /X+3]`
echo %@fmtdate[`%d/%m/%Y`]

seems to work.

I'd never thought of using SETDOS like this before - its effects on
parsing seemed to imply to me that it had to be used by the caller,
not the callee, which is almost never what I want.

Thanks for the hint.
Paul.
Thanks,
Paul.
 

Similar threads

Back
Top