Pergunta

Eu tenho um bloco de texto que im tomada a partir de um Gedcom ( Aqui e Aqui ) Arquivo

O texto é plana e basicamente dividido em "nós"

eu sou dividindo cada nó sobre o caractere \ r e subdividindo-a, assim, em cada uma das suas partes (quantidade de "linhas" pode variar)

Eu sei que a 0 endereço será sempre o ID mas depois que tudo pode estar em qualquer lugar assim que eu quero testar cada célula da matriz para ver se ele contém a tag correta para mim proccess

um exemplo do que dois nós seria parecido

0 @ind23815@ INDI <<<<<<<<<<<<<<<<<<< Start of node 1
1 NAME Lawrence /Hucstepe/
2 DISPLAY Lawrence Hucstepe
2 GIVN Lawrence
2 SURN Hucstepe
1 POSITION -850,-210
2 BOUNDARY_RECT (-887,-177),(-813,-257)
1 SEX M
1 BIRT 
2 DATE 1521
1 DEAT Y
2 DATE 1559
1 NOTE     * Born: Abt 1521, Kent, England
2 CONT     * Marriage: Jane Pope 17 Aug 1546, Kent, England
2 CONT     * Died: Bef 1559, Kent, England
2 CONT 
1 FAMS @fam08318@
0 @ind23816@ INDI  <<<<<<<<<<<<<<<<<<<<<<< Start of Node 2
1 NAME Jane /Pope/
2 DISPLAY Jane Pope
2 GIVN Jane
2 SURN Pope
1 POSITION -750,-210
2 BOUNDARY_RECT (-787,-177),(-713,-257)
1 SEX F
1 BIRT 
2 DATE 1525
1 DEAT Y
2 DATE 1609
1 NOTE     * Born: Abt 1525, Tenterden, Kent, England
2 CONT     * Marriage: Lawrence Hucstepe 17 Aug 1546, Kent, England
2 CONT     * Died: 23 Oct 1609
2 CONT 
1 FAMS @fam08318@
0 @ind23817@ INDI  <<<<<<<<<<< start of Node 3

So a when im done i have an array that looks like

address , string
0 = "1 NAME Lawrence /Hucstepe/"
1 = "2 DISPLAY Lawrence Hucstepe"
2 = "2 GIVN Lawrence"
3 = "2 SURN Hucstepe"
4 = "1 POSITION -850,-210"
5 = "2 BOUNDARY_RECT (-887,-177),(-813,-257)"
6 = "1 SEX M"
7 = "1 BIRT "
8 = "1 FAMS @fam08318@"

So my question is what is the best way to search the above array to see which Cell has the SEX tag or the NAME Tag or the FAMS Tag

this is the code i have

private int FindIndexinArray(string[] Arr, string search)
{
    int Val = -1;
    for (int i = 0; i < Arr.Length; i++)
    {
        if (Arr[i].Contains(search))
        {
            Val = i;
        }
    }
    return Val;
}

But it seems inefficient because i end up calling it twice to make sure it doesnt return a -1

Like so

if (FindIndexinArray(SubNode, "1 BIRT ") != -1) { // add birthday to Struct I.BirthDay = SubNode[FindIndexinArray(SubNode, "1 BIRT ") + 1].Replace("2 DATE ", "").Trim(); }

Lamentamos este é um post mais longo, mas espero que vocês terão alguns conselhos de especialistas

Foi útil?

Solução

Pode usar o FindAll método estático da classe Array: Ele irá retornar a corda em si, porém, se isso funciona ..

string[] test = { "Sex", "Love", "Rock and Roll", "Drugs", "Computer"};
Array.FindAll(test, item => item.Contains("Sex") || item.Contains("Drugs") || item.Contains("Computer"));

O => indica uma expressão lambda. Basicamente, um método sem uma implementação concreta. Você também pode fazer isso se o lambda lhe dá arrepios.

//Declare a method 

     private bool HasTag(string s)
     {
         return s.Contains("Sex") || s.Contains("Drugs") || s.Contains("Computer");
     }

     string[] test = { "Sex", "Love", "Rock and Roll", "Drugs", "Computer"};
     Array.FindAll(test, HasTag);

Outras dicas

Que tal um simples expressão regular?

^(\d)\s=\s\"\d\s(SEX|BIRT|FAMS){1}.*$

capturas primeiro grupo o endereço, segundo grupo a tag.

Além disso, ele pode ser mais rápido para despejar todos os itens de matriz para uma string e fazer o seu regex em todo o lote de uma só vez.

"Mas parece ineficiente porque eu acabar chamando-o duas vezes para se certificar de ele não retornar uma -1"

Copiar o valor retornado a uma variável antes de testar para evitar várias chamadas.

IndexResults = FindIndexinArray(SubNode, "1 BIRT ")
if (IndexResults != -1)
        {
            // add birthday to Struct 
            I.BirthDay = SubNode[IndexResults].Replace("2 DATE ", "").Trim();
        }

O laço for no método FindIndex Matriz e quebrar uma vez que você encontrar uma correspondência Se você estiver interessado em apenas o primeiro jogo.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top