Skip to content

Commit fe98946

Browse files
committed
Better label2indexing. Fixes #74
1 parent e74d9c5 commit fe98946

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ComponentArrays"
22
uuid = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
33
authors = ["Jonnie Diegelman <47193959+jonniedie@users.noreply.github.com>"]
4-
version = "0.9.1"
4+
version = "0.9.2"
55

66
[deps]
77
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"

src/plot_utils.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,12 @@ julia> label2index(x, "b[1]")
8484
8585
see also `labels`
8686
"""
87-
label2index(x::ComponentVector, str::AbstractString) = label2index(labels(x), str)
88-
label2index(labs, str::AbstractString) = findall(startswith.(labs, str))
87+
label2index(x::ComponentVector, str) = label2index(labels(x), str)
88+
function label2index(labs, str)
89+
idx = findall(startswith.(labs, str * r"(\.|\[)"))
90+
if !isempty(idx)
91+
return idx
92+
else
93+
return [findfirst(l -> l .== str, labs)]
94+
end
95+
end

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,16 @@ end
392392
"c.b[2,2]",
393393
]
394394
@test label2index(ca2, "c.b") == collect(11:14)
395+
396+
# Issue #74
397+
lab2 = labels(ComponentArray(a=1, aa=ones(2), ab=[(a=1, aa=ones(2)), (a=1, aa=ones(2))], ac=(a=1, ab=ones(2,2))))
398+
@test label2index(lab2, "a") == [1]
399+
@test label2index(lab2, "aa") == collect(2:3)
400+
@test label2index(lab2, "ab") == collect(4:9)
401+
@test label2index(lab2, "ab[1].aa") == collect(5:6)
402+
@test label2index(lab2, "ac") == collect(10:14)
403+
@test label2index(lab2, "ac.a") == [10]
404+
@test label2index(lab2, "ac.ab") == collect(11:14)
395405
end
396406

397407
@testset "Uncategorized Issues" begin

0 commit comments

Comments
 (0)