SteakStyles
SteakStyles

Reputation: 87

Julia: Array manipulation

I'm having trouble with removing a specific element from an array in Julia.

I have an array of Job objects, each with their own attributes, and as a user answers a questionnaire, I want to remove the jobs from the array that does not match the users preferences.

jobs = [retail, construction, nurse, programmer, engineer, law]

Basically, if, for example, the user does not to work outside, I would want to remove construction.

after looking at the julia documentation, there does not seem to be a method for simply removing a specified value from a basic array. Is there any other way to do this using another data structure?

Upvotes: 3

Views: 263

Answers (2)

David P. Sanders
David P. Sanders

Reputation: 5325

I think a Set is a good option for a data structure for this purpose, e.g.

jobs = Set(["retail", "construction", "nurse", "programmer"])

delete!(jobs, "nurse")
push!(jobs, "teacher")

Upvotes: 4

Dan Getz
Dan Getz

Reputation: 18227

One option could be:

jobs = filter(x->x != "construction",jobs)

Another method:

splice!(jobs,findfirst(x->x=="construction",jobs)

A third option:

jobs = jobs[map(x->x!="construction",jobs)]

I've assumed the array elements are strings, although in the question they appear as Job typed variables, but the conditions are all general predicates - so will work with appropriate modification.

Upvotes: 0

Related Questions