Select to view content in your preferred language

Not sure on functionality needed for AGOL popup expression

138
2
Jump to solution
2 weeks ago
OliviaMancuso
Emerging Contributor

Hello, I am working with a field that includes numeric values as a string that appear like this: 0820;0830;0840;0850;0860;0870A;0880;0890;1050;1060;1070;1080;1100;1110;1120;1140;1150;1160.

What I would like to do is separate each value on a new line and remove leading zero/one trailing zero in my popup. I am working with the arcade script below. So far the script is working as I get a list like the following:

OliviaMancuso_0-1737470967536.png

But the problem I am having is that I don't know how to deal with values that also include a letter. In those rare cases, it can be any letter (always at the end), and I don't know how to remove the zero that is to the left of the letter. So right now in the resulting list it looks like 870A when I would like it to look like 87A.

OliviaMancuso_1-1737471024892.png

I cannot claim to have written the script below as I am very new to arcade. I was using Esri's AI assistant to help form the script. But I was not having any luck with the more complicated value mentioned above. Any advice that could be provided would be much appreciated. Thanks!

var inputString = $feature.OrdConcat;
var values = Split(inputString, ";");
var result = [];

for (var i in values) {
    var cleanedValue = values[i];
    
    // Remove the first leading zero
    if (Left(cleanedValue, 1) == "0") {
        cleanedValue = Mid(cleanedValue, 1, Count(cleanedValue) - 1);
    }
    
    // Check if the last character is a letter
    if (IsNan(Right(cleanedValue, 1))) {
        // Check if the character before the letter is a zero
        if (Mid(cleanedValue, Count(cleanedValue) - 2, 1) == "0") {
            cleanedValue = Left(cleanedValue, Count(cleanedValue) - 2) + Right(cleanedValue, 1);
        }
    } else if (Right(cleanedValue, 1) == "0") {
        // Remove exactly one trailing zero if no letter is present at the end
        cleanedValue = Left(cleanedValue, Count(cleanedValue) - 1);
    }
    
    Push(result, cleanedValue);
}

return Concatenate(result, TextFormatting.NewLine);

 

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

In line 14, you're checking if a string is a NaN. That will return false, since the function IsNAN evaluates if a value is a NaN according to these rules:

A number is considered NaN in one of the following scenarios: - 0/0 - Infinity / Infinity - Infinity * 0 - Any operation in which NaN is an operand - Casting a non-numeric text or undefined to a number

So what you have to do is to attempt to cast that last character as a Number and check if it's a NaN. This will return the list as you wanted.

 

var inputString = $feature.OrdConcat;
var values = Split(inputString, ";");
var result = [];

for (var i in values) {
  var cleanedValue = values[i];

  // Remove the first leading zero
  if (Left(cleanedValue, 1) == "0") {
    cleanedValue = Mid(cleanedValue, 1, Count(cleanedValue) - 1);
  }

 // Check if the last character is a letter
  if (IsNan(Number(Right(cleanedValue, 1)))) {

    // Check if the character before the letter is a zero
    if (Mid(cleanedValue, Count(cleanedValue) - 2, 1) == "0") {
      cleanedValue = Left(cleanedValue, Count(cleanedValue) - 2) +
      Right(cleanedValue, 1);
   }
  } else if (Right(cleanedValue, 1) == "0") {
    // Remove exactly one trailing zero if no letter is present at the end
    cleanedValue = Left(cleanedValue, Count(cleanedValue) - 1);
  }

  Push(result, cleanedValue);
}

return Concatenate(result, TextFormatting.NewLine);

 

 

View solution in original post

2 Replies
KenBuja
MVP Esteemed Contributor

In line 14, you're checking if a string is a NaN. That will return false, since the function IsNAN evaluates if a value is a NaN according to these rules:

A number is considered NaN in one of the following scenarios: - 0/0 - Infinity / Infinity - Infinity * 0 - Any operation in which NaN is an operand - Casting a non-numeric text or undefined to a number

So what you have to do is to attempt to cast that last character as a Number and check if it's a NaN. This will return the list as you wanted.

 

var inputString = $feature.OrdConcat;
var values = Split(inputString, ";");
var result = [];

for (var i in values) {
  var cleanedValue = values[i];

  // Remove the first leading zero
  if (Left(cleanedValue, 1) == "0") {
    cleanedValue = Mid(cleanedValue, 1, Count(cleanedValue) - 1);
  }

 // Check if the last character is a letter
  if (IsNan(Number(Right(cleanedValue, 1)))) {

    // Check if the character before the letter is a zero
    if (Mid(cleanedValue, Count(cleanedValue) - 2, 1) == "0") {
      cleanedValue = Left(cleanedValue, Count(cleanedValue) - 2) +
      Right(cleanedValue, 1);
   }
  } else if (Right(cleanedValue, 1) == "0") {
    // Remove exactly one trailing zero if no letter is present at the end
    cleanedValue = Left(cleanedValue, Count(cleanedValue) - 1);
  }

  Push(result, cleanedValue);
}

return Concatenate(result, TextFormatting.NewLine);

 

 

OliviaMancuso
Emerging Contributor

That is working perfectly! Thank you for the help, it is much appreciated 🙂

Here are the results:

OliviaMancuso_0-1737480105490.png

 

0 Kudos