Pattern matching error

hi could someone please help me to create a correct pattern matching this is a example of the type:

> [{%Master{__meta__: #Ecto.Schema.Metadata<:loaded, "seligibility", "patient">,
>    date_of_birth: nil, first_name: nil, gender: nil, id: 1, language_code: nil,
>    last_name: nil, middle_name: nil,
>    mod_start_date: %DateTime{calendar: Calendar.ISO, day: 12, hour: 0,
>     microsecond: {0, 6}, minute: 0, month: 12, second: 0, std_offset: 0,
>     time_zone: "Etc/UTC", utc_offset: 0, year: 1994, zone_abbr: "UTC"},
>    patient_num: "12345678", person_code: "LLC", prefix: nil,
>    relationship_code: nil, ssn: nil, test_only: nil},
>   [%SON{__meta__: #Ecto.Schema.Metadata<:loaded, "seligibility", "patient_eligibility_list">,
>     coverage_type: "FAM", end_date: nil, group_id: 1, item_id: 1, parent_id: 1,
>     start_date: nil,
>     updated_at: %DateTime{calendar: Calendar.ISO, day: 2, hour: 0,
>      microsecond: {0, 6}, minute: 0, month: 12, second: 0, std_offset: 0,
>      time_zone: "Etc/UTC", utc_offset: 0, year: 1995, zone_abbr: "UTC"}},
>    %SON{__meta__: #Ecto.Schema.Metadata<:loaded, "seligibility", "patient_eligibility_list">,
>     coverage_type: "FAM", end_date: nil, group_id: 1, item_id: 2, parent_id: 1,
>     start_date: nil,
>     updated_at: %DateTime{calendar: Calendar.ISO, day: 2, hour: 0,
>      microsecond: {0, 6}, minute: 0, month: 12, second: 0, std_offset: 0,
>      time_zone: "Etc/UTC", utc_offset: 0, year: 1995, zone_abbr: "UTC"}}]}]

I was using: [{master, [son]}], and it’s incorrect

idon’t know how to fix it.

Many people here are happy to help. The problem is, you didn’t actually paste an error. You just pasted a list with a struct value.

This doesn’t mean anything. Where are you using it? What about it is incorrect?

look the error:

(MatchError) no match of right hand side value: [{%NA.DB.Schema.Patient{meta: ecto.Schema.Metadata<:loaded, “seligibility”, “patient”>, date_of_birth: nil, first_name: nil, gender: nil, id: 1, language_code: nil, last_name: nil, middle_name: nil, mod_start_date: %DateTime{calendar: Calendar.ISO, day: 12, hour: 0, microsecond: {0, 6}, minute: 0, month: 12, second: 0, std_offset: 0, time_zone: “Etc/UTC”, utc_offset: 0, year: 1994, zone_abbr: “UTC”}, patient_num: “12345678”, person_code: “LLC”, prefix: nil, relationship_code: nil, ssn: nil, test_only: nil}, [%NA.DB.Schema.Patient_eligibility_list{meta: ecto.Schema.Metadata<:loaded, “seligibility”, “patient_eligibility_list”>, coverage_type: “FAM”, end_date: nil, group_id: 1, item_id: 1, parent_id: 1, start_date: nil, updated_at: %DateTime{calendar: Calendar.ISO, day: 2, hour: 0, microsecond: {0, 6}, minute: 0, month: 12, second: 0, std_offset: 0, time_zone: “Etc/UTC”, utc_offset: 0, year: 1995, zone_abbr: “UTC”}}, %NA.DB.Schema.Patient_eligibility_list{meta: ecto.Schema.Metadata<:loaded, “seligibility”, “patient_eligibility_list”>, coverage_type: “FAM”, end_date: nil, group_id: 1, item_id: 2, parent_id: 1, start_date: nil, updated_at: %DateTime{calendar: Calendar.ISO, day: 2, hour: 0, microsecond: {0, 6}, minute: 0, month: 12, second: 0, std_offset: 0, time_zone: “Etc/UTC”, utc_offset: 0, year: 1995, zone_abbr: “UTC”}}]}]
(na_adj) lib/na_adj/patient_eligibility.ex:29: NA.Adj.PatientEligibility.verify_patient_eligibility/4

i was pattern match the list at the first to this: [{master,[son]}]

Try [{master, sons}] or [{master, [son1, son2]}] or [{master, [son | _]}]. In your first example you have two sons, but your pattern [{master, [son]}] expects exactly one.

1 Like

One I learned from looking at some erlang code:

[{master, [_|_]=sons}]

I know some people might think it’s ugly, but I like that it explicitly says “hey I’m trying to match a list”.

It is not ugly, but it does say that you are trying to match a list of at least one element so it will not match no sons, the empty list. Depending on where you are doing this matching you could replace it with the pattern [{master,sons}] with a guard is_list(sons).