Skip to main content
Inspiring
November 6, 2024
Answered

Import and verify CSV data

  • November 6, 2024
  • 3 replies
  • 2863 views

Hello everyone, I hope you are well.

I'm trying to make a code read in a CSV document, properly filled in just one column.
At first, the code is correctly reading the data, however, when checking the typed text and the data for comparison and approval, it reports as "not found".

How it works is very simple, I type the text into an Input, and use a button to check.

 

function processarCSV(dados:String):void 
{
    var linhas:Array = dados.split("\n"); // Divide o CSV em linhas
    var valorDigitado:String = insert_code.text; // Valor digitado pelo usuário
    var valorEncontrado:Boolean = false; // Variável para verificar se o valor foi encontrado
    
    for (var i:int = 0; i < linhas.length; i++) 
	{
        var colunas:Array = linhas[i].split(","); // Divide cada linha em colunas
		trace("" + colunas[0]);
        
        // Verifica se o valor digitado pelo usuário está em alguma coluna
        for (var j:int = 0; j < colunas.length; j++) 
		{
            if (colunas[j] == valorDigitado) 
			{
                trace("Valor encontrado na linha " + (i + 1) + ", coluna " + (j + 1));
                valorEncontrado = true;
                break;
            }
        }
        
        if (valorEncontrado) {
            break; // Sair do loop de linhas se o valor foi encontrado
        }
    }
    
    if (valorEncontrado) {
        trace("O valor \"" + valorDigitado + "\" foi encontrado no CSV.");
    } else {
        trace("O valor \"" + valorDigitado + "\" não foi encontrado no CSV.");
    }
}

 

 

log trace:

[SWF] teste_csv.swf - 2686 bytes after decompression

// data loaded correctly from csv
aaaa
bbbb
cccc
dddd
eeee

The value "cccc" was not found in the CSV. // When clicking the button

 

If you could help me, I would be very grateful.

Thank you very much in advance.

This topic has been closed for replies.
Correct answer JoãoCésar17023019

Why not just check for the search query in the string obtained from the CSV?

// assuming that this is the data read from the CSV file
var csvString:String = "aaaa\nbbbb\ncccc\ndddd\neeee\n";

function search(e:MouseEvent):void
{
	trace("value: " + (queryTF.text && csvString.indexOf(queryTF.text) > -1 ? "found" : "not found"));
	queryTF.text = "";
}

searchButton.addEventListener(MouseEvent.CLICK, search);

 

3 replies

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
November 6, 2024

Why not just check for the search query in the string obtained from the CSV?

// assuming that this is the data read from the CSV file
var csvString:String = "aaaa\nbbbb\ncccc\ndddd\neeee\n";

function search(e:MouseEvent):void
{
	trace("value: " + (queryTF.text && csvString.indexOf(queryTF.text) > -1 ? "found" : "not found"));
	queryTF.text = "";
}

searchButton.addEventListener(MouseEvent.CLICK, search);

 

kglad
Community Expert
Community Expert
November 6, 2024

even better 

JoãoCésar17023019
Community Expert
Community Expert
November 6, 2024

Hi.

 

What's the content of your CSV file?

 

Regards,

JC

vvvverTAuthor
Inspiring
November 6, 2024

All good?

The CSV only has 5 lines, with random characters. I have attached a screenshot.

The idea would be for me to type a value in the Text Input, and check in the CSV if that value exists.

kglad
Community Expert
Community Expert
November 6, 2024

how are you checking?

kglad
Community Expert
Community Expert
November 6, 2024

what's clicking your button do?

what's that function supposed to do?

vvvverTAuthor
Inspiring
November 6, 2024

All good? Sorry for the lack of information.

The button calls the callback mentioned above. The purpose of which is to check a CSV file and inform if there is a value similar to the one entered in the Text Input.

With this, it will forward a message with "value found" or "value not found".