I’m looking to convert a digit to its position in the alphabet, given a data field
Knowing that I need the following alphabet
ABCDEFGHJKLMNPRSTUVWXYZ
Ex: 1 becomes A
2 becomes B…
I tried the positionInAlphabet function in the extraction step, but I’m new to java.
Is it a single letter you are extracting from your data file by the Extraction Step or a whole word or whole sentence? Can you for example share a couple of examples of the extracted data?
Good morning
It’s the opposite, in my data field I have numbers ranging from 1 to 23, I’m looking to convert the number to its position in the alphabet
1 becomes A
2 becomes B…
What kind of data file are you using? Is it a XML or CSV data file, for example? In case it is a CSV data file then you can change the Based On option of your Extraction Step to JavaScript and apply the following JavaScript code to the Expression text area to solve your request:
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var field = data.extract("columnName", 0);
var result = "";
if (field !== "" && !isNaN(field)) {
var position = parseInt(field) - 1; // Index starts from 0
if (position < 26) {
result = alphabet.charAt(position);
}
}
result;
NOTE: Replace columnName with the correct column name
Indeed it works, but I would like to attach it to a Javascript field, and not to the database. the Base is in csv.
I made a calculation upstream in java compared to the base, maybe we can integrate it directly.
I am attaching the client’s brief, it may be clearer
I’m sorry but I can’t quite following you. Can you let me know please what you mean by “[…] but I would like to attach it to a JavaScript field, and not to the database”?
Please note that the provided JavaScript code will assign a value like 2 or 22 to a record field when the extracted value is something like B or V.
According to the provided screenshot it isn’t entirely clear to me what you would like to achieve.
I see that you have applied the expression record.fields.SEQUENCE_ALIZE * 10 / 23 for the extraction of record field Calcul_Alize
I see that you have applied the expression (record.fields.SEQUENZE_ALIZE * 10) - (record.fields.Calcul_Alize) + 1 for the extraction of record field Calcul_Fin_Alize
But which expression would you like to apply for the extraction of record field Conversion_Alize?
According to the provided screenshots I assume that you will have to change the earlier provided JavaScript code example to the following to achieve what you want:
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var field = record.fields.Calcul_Fin_Alize;
var result = "";
if (field !== "" && !isNaN(field)) {
var position = parseInt(field) - 1; // Index starts from 0
if (position < 26) {
result = alphabet.charAt(position);
}
}
result;
P.S. The reason why an error is shown on line 2 of the Expression text area of record field Conversion_Alize is because the first column of your CSV file does not have the name Calcul_Fin_Alize.