How to store split values separately in another strings

HI,

This is Lalarukh, I have a long input which I’ve stored in a string.
I know how to split the array values but I don’t know after splitting string into 2 parts then how can I store the first value of split into separate string & another into separate string.

 def process() do
    input = "Sun 57909227 Mercury
    Earth 384400 Moon
    Sun 149598262 Earth
    Sun Moon
    Deimos Moon
    Deimos
    Deimos Phobos
    Moon
    LROrbiter"
    distance(input)
  end

  defp distance(input) do
    d_spltng =  String.splitter(input, [" ", ","]) |> Enum.take(10)
    IO.puts(d_spltng)
   end

From distance function I can save the value till Earth in d_spltng
But don’t know how to save the rest (From Sun Moon to LROrbiter) into another string.
Seeking your response.

I’ve created a gist with one approach you can take. A couple of thoughts:

  1. I find it clearer to parse data into a structured form first, then add meaning to it
  2. Working out a good structure for the data helps develop a more robust framework for later analysis. In this example, a graph data structure would be good since the vertices would be the bodies of interest and the edges would be the weight (distance) between them.

Using the iinked module, the following examples reflect this thinking:

# Parse the data into a canonical form. This form happens
# to be compatible with the edge definition in the `lib graph`
# library.
iex> parsed = Distance.parse                     
[
  {"Sun", "Mercury", [weight: 57909227]},
  {"Earth", "Moon", [weight: 384400]},
  {"Sun", "Earth", [weight: 149598262]},
  {"Sun", "Moon", [weight: nil]},
  {"Deimos", "Moon", [weight: nil]},
  {"Deimos", nil, [weight: nil]},
  {"Deimos", "Phobos", [weight: nil]},
  {"Moon", nil, [weight: nil]},
  {"LROrbiter", nil, [weight: nil]}
]
# Now we can convert this list of parsed data into
# a graph using `libgraph`.
iex> g = Distance.build_graph(parsed)            
#Graph<type: directed, vertices: ["Sun", "Mercury", "Earth", "Moon"], edges: ["Sun" -> "Mercury", "Sun" -> "Earth", "Earth" -> "Moon"]>

# Now we can ask questions of the data
iex> Distance.distances_from g, "Earth"          
[%Graph.Edge{label: nil, v1: "Earth", v2: "Moon", weight: 384400}]

iex> Distance.distances_from g, "Sun"  
[
  %Graph.Edge{label: nil, v1: "Sun", v2: "Mercury", weight: 57909227},
  %Graph.Edge{label: nil, v1: "Sun", v2: "Earth", weight: 149598262}
]

# We can also filter the parsed data to get only those
# elements that have a distance
iex> Distance.with_distance parsed                    
[
  {"Sun", "Mercury", [weight: 57909227]},
  {"Earth", "Moon", [weight: 384400]},
  {"Sun", "Earth", [weight: 149598262]}
]

# Or don't have a distance...
iex> Distance.with_no_distance parsed
[
  {"Sun", "Moon", [weight: nil]},
  {"Deimos", "Moon", [weight: nil]},
  {"Deimos", nil, [weight: nil]},
  {"Deimos", "Phobos", [weight: nil]},
  {"Moon", nil, [weight: nil]},
  {"LROrbiter", nil, [weight: nil]}
]
1 Like