• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Import and verify CSV data

Explorer ,
Nov 05, 2024 Nov 05, 2024

Copy link to clipboard

Copied

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.

TOPICS
ActionScript , Code , Error , Import and export

Views

687

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 06, 2024 Nov 06, 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);

 

Votes

Translate

Translate
Community Expert ,
Nov 06, 2024 Nov 06, 2024

Copy link to clipboard

Copied

what's clicking your button do?

what's that function supposed to do?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 06, 2024 Nov 06, 2024

Copy link to clipboard

Copied

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".

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2024 Nov 06, 2024

Copy link to clipboard

Copied

Hi.

 

What's the content of your CSV file?

 

Regards,

JC

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 06, 2024 Nov 06, 2024

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2024 Nov 06, 2024

Copy link to clipboard

Copied

how are you checking?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2024 Nov 06, 2024

Copy link to clipboard

Copied

actually, it looks like that function is supposed to check, but it's not valid.  use

 

var code_valid: Boolean
function processarCSV(dados: String): void {
code_valid = false;
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 < dados.length; i++) {
if(insert_code.text == linhas[i]) {
code_valid = true;
break;
}
}
trace(code_valid);
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 06, 2024 Nov 06, 2024

Copy link to clipboard

Copied

Even with changing the callback, the problem persists. The value exists in the CSV (loaded in the trace), however, when typing it and pressing the button, it shows as false. I attached an image.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2024 Nov 06, 2024

Copy link to clipboard

Copied

your loaded file isn't what you think it is or you need to split on \r, too

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 06, 2024 Nov 06, 2024

Copy link to clipboard

Copied

The file is correct, it is carrying the same data that everyone has. 

Add "\r"? I didn't understand @kglad 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2024 Nov 06, 2024

Copy link to clipboard

Copied

like your split using \n

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 06, 2024 Nov 06, 2024

Copy link to clipboard

Copied

Changing the \n to \e didn't solve the problem either. It is still presented as if the value entered was not found in the CSV.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2024 Nov 06, 2024

Copy link to clipboard

Copied

e?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2024 Nov 06, 2024

Copy link to clipboard

Copied

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);

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2024 Nov 06, 2024

Copy link to clipboard

Copied

even better 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 06, 2024 Nov 06, 2024

Copy link to clipboard

Copied

Hi @JoãoCésar 

 

But then if I need to add a new value, I will need to go to the project's source code, where the idea is to add it externally through CSV.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2024 Nov 06, 2024

Copy link to clipboard

Copied

no, just add it to your csv file.  don't worry.  just use the search() with your current csv and see.  extraneous characters don't matter (unless the match someone's input which is next to impossible).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 06, 2024 Nov 06, 2024

Copy link to clipboard

Copied

This way really works, but I still don't understand how the CSV file will be loaded externally, if I need to fill in the String in the source code.

In the code provided, there is no

csvString:String = "dados.csv";

 

It was replaced by:

csvString:String = "yyyy\nbbbbb\ncccc\ndddd\neeee\n";

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2024 Nov 06, 2024

Copy link to clipboard

Copied

just load your csv file like you were before and then assign and use (in the indexOf) the string of the file's contents.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 07, 2024 Nov 07, 2024

Copy link to clipboard

Copied

It worked, thank you very much for your attention and patience.
@kglad @JoãoCésar 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 07, 2024 Nov 07, 2024

Copy link to clipboard

Copied

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 07, 2024 Nov 07, 2024

Copy link to clipboard

Copied

LATEST

You're welcome!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines