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.
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);
Copy link to clipboard
Copied
what's clicking your button do?
what's that function supposed to do?
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".
Copy link to clipboard
Copied
Hi.
What's the content of your CSV file?
Regards,
JC
Copy link to clipboard
Copied
Copy link to clipboard
Copied
how are you checking?
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);
}
Copy link to clipboard
Copied
Copy link to clipboard
Copied
your loaded file isn't what you think it is or you need to split on \r, too
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
Copy link to clipboard
Copied
like your split using \n
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.
Copy link to clipboard
Copied
e?
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);
Copy link to clipboard
Copied
even better
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.
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).
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";
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.
Copy link to clipboard
Copied
It worked, thank you very much for your attention and patience.
@kglad @JoãoCésar
Copy link to clipboard
Copied
you're welcome.
Copy link to clipboard
Copied
You're welcome!