empty-slice iterate

This commit is contained in:
John Kerl 2020-11-25 14:13:34 -05:00
parent 13def83aaa
commit 7fc4d231fb
5 changed files with 86 additions and 4 deletions

View file

@ -1,4 +1,4 @@
mlr -n --ojson put '
run_mlr -n --ojson put '
end {
a = [1,2,3,4,5,6,7];
m = {"a": 1, "b": 2};

View file

@ -21,7 +21,7 @@ mlr put $y = substr($x, 3, 3)
x=abcdefg,y=c
mlr put $y = substr($x, 4, 3)
x=abcdefg,y=
x=abcdefg,y=(error)
mlr put $y = substr($x, 2, 5)
x=1234567,y=2345

View file

@ -1 +1,73 @@
mlr -n --ojson put
end {
a = [1,2,3,4,5,6,7];
m = {"a": 1, "b": 2};
s = "abcdefg";
emit {
"a35": a[3:5],
"a37": a[3:7],
"a17": a[1:7],
"a07": a[0:7],
"a39": a[3:9],
"a53": a[5:3],
"a93": a[9:3],
};
emit {
"m11": m[1:1],
};
emit {
"s35": s[3:5],
"s37": s[3:7],
"s17": s[1:7],
"s07": s[0:7],
"s39": s[3:9],
"s53": s[5:3],
"s93": s[9:3],
};
emit {
"u35": substr(s,3,5),
"u37": substr(s,3,7),
"u17": substr(s,1,7),
"u07": substr(s,0,7),
"u39": substr(s,3,9),
"u53": substr(s,5,3),
"u93": substr(s,9,3),
};
}
{
"a35": [3, 4, 5],
"a37": [3, 4, 5, 6, 7],
"a17": [1, 2, 3, 4, 5, 6, 7],
"a07": (error),
"a39": (error),
"a53": [],
"a93": (error)
}
{
"m11": (error)
}
{
"s35": "cde",
"s37": "cdefg",
"s17": "abcdefg",
"s07": "",
"s39": "",
"s53": (error),
"s93": ""
}
{
"u35": "cde",
"u37": "cdefg",
"u17": "abcdefg",
"u07": "",
"u39": "",
"u53": (error),
"u93": ""
}

View file

@ -155,11 +155,19 @@ func (this *ArraySliceAccessNode) Evaluate(state *State) types.Mlrval {
lowerIndex, ok := lowerIndexMlrval.GetIntValue()
if !ok {
return types.MlrvalFromError()
if lowerIndexMlrval.IsEmpty() {
lowerIndex = 1
} else {
return types.MlrvalFromError()
}
}
upperIndex, ok := upperIndexMlrval.GetIntValue()
if !ok {
return types.MlrvalFromError()
if upperIndexMlrval.IsEmpty() {
upperIndex = int64(len(array))
} else {
return types.MlrvalFromError()
}
}
lowerZindex, ok := types.UnaliasArrayIndex(&array, lowerIndex)

View file

@ -5,6 +5,7 @@ small:
* justWroteEmptyLine -> pprint too ...
! nidx @ 10-min
! text-put @ 10-min
! slice: empty lower/higher
medium:
! gating after: 'int x = 1; x = "abc"'
@ -377,3 +378,4 @@ NITS/NON-IMMEDIATE:
o ....
o put '$array = [1,2,3,[4,5]]' is a syntax error unfortunately; need '$array = [1,2,3,[4,5] ]'
* doc: string index/slice access on RHS
* substr(7,5) -- "" or error?