On Mon, 24 Jun 2019, Riccardo (Jack) Lucchetti wrote:
On Sun, 23 Jun 2019, Allin Cottrell wrote:
> sscanf() could also do the job (element by element). But it might be nice to
> enable something like
>
> matrix m
> sscanf(S, "%m", m)
>
> to scan an array of numeric strings (S) into a matrix in one go.
Thank you, this is very useful. The version that is in git now, however, does
not handle non-numeric entries, which could be a bit of a nuisance sometimes.
Example:
<hansl>
c = defarray("2", "1.0", "NA", "1")
m = {}
sscanf(c, "%m", &m)
print m
</hansl>
returns
<output>
m (2 x 1)
2
1
</output>
However, before plunging into the code, I'd like to point out that this is
just a special case of
<hansl>
s = sprintf("1 2\nfoobarbaz 3")
m = {}
sscanf(s, "%m", &m)
print m
</hansl>
which returns (rightly, IMO)
<output>
m (1 x 2)
1 2
</output>
so I'm not sure what we should do. Opinions?
I've just been working on this, replacing my original code with
something more efficient. In current git this is what happens:
<hansl>
? strings S = defarray("1", "2", "egg", "57",
"spoon")
? matrix m
? eval sscanf(S, "%m", m)
3
? print m
m (5 x 1)
1
2
nan
57
nan
</hansl>
That is, you get a column vector of (default) length equal to the
length of the array of strings, where any strings that don't convert
correctly to numeric values give nan (NA) in the output. The return
value from sscanf() tells you how many successful conversions were
made.
As you point out, this is not the same behavior as with plain string
input and matrix (%m) conversion. I'm not sure which way we should go,
but I think there's a case for "back-porting" the new behavior.
Allin