On Sun, 11 Dec 2016, Allin Cottrell wrote:
I'm wondering whether people would consider this function (which
I'm calling
"strscrape" for now) worth having: it takes a string argument and returns a
row vector containing any numerical values encountered in the string (or an
empty matrix if none are found). There's a test version in git but it's not
documented; I'm not committed to keeping it if doesn't attract much support.
I, for one, quite like it.
Unlike using sscanf() to retrieve a matrix, the idea with this
function is
that the numbers may be embedded in arbitrary text. Numbers are identified as
starting with either a minus sign (followed directly by a digit) or a digit,
and their length in characters is decided by the C function strtod (so
scientific notation is acceptable). As the function stands, the decimal
character is unconditionally '.'.
Now, how could I EVER object to THIS? ;)
Of course some rough edges will have to be dealt with. For example, the
output to
a = strscrape("1.2.3")
is, as of now,
[1.2, 3]
which of course makes sense but is only one of the possible conventions.
One use for such a function would be "remedial" -- getting
values back from a
gretl command when there's no relevant accessor. Here's a semi-general
illustration:
<hansl>
function matrix scrapevals (const string cmd, const list L[null])
string fname = sprintf("%s/_tmp.txt", $dotdir)
set force_decpoint on
outfile @fname --write --quiet
@cmd
outfile --close
string s = readfile(fname)
remove(fname)
return strscrape(s)
end function
open data4-10
list L = ENROLL CATHOL
matrix m = scrapevals("corr L --kendall", L)
print m
</hansl>
Actually, the function above would be even better if one used the --buffer
option to outfile, but I don't mean to be picky.
However, it could also be useful for processing text other than
gretl's own
output. Here's a generic example:
<hansl>
string s="Here is some text 1.5e-4with embedded 12345 numbers \
stuck int0 1t."
matrix vals = strscrape(s)
print vals
</hansl>
which gives
vals (1 x 4)
0.00015000 12345. 0.0000 1.0000
Very nice.
-------------------------------------------------------
Riccardo (Jack) Lucchetti
Dipartimento di Scienze Economiche e Sociali (DiSES)
Università Politecnica delle Marche
(formerly known as Università di Ancona)
r.lucchetti(a)univpm.it
http://www2.econ.univpm.it/servizi/hpp/lucchetti
-------------------------------------------------------