Rearrange string with Attribute rule

1949
8
Jump to solution
11-06-2020 01:09 AM
KARIMLABIDI
Occasional Contributor

Hello, 

I'm a newbie on Arcade, i very like this language but it's new for me and I have some difficulties to find my happiness!

I would like to rearrange a string Address in a field, I would like put the last word in 1st place and the other after (in parenthesis)

For example: RUE GEORGES BONNAC will be BONNAC (RUE GEORGES). 

I Think, I have to split my string et count the words but i got a dictionary error and I don't what to do.

Thank you everybody.

0 Kudos
2 Solutions

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Hi KARIM LABIDI , 

I think this could do the trick:

var township = FeatureSetByName($datastore, "GDB.SIGWRITER.FV_TRONC_L", ["NOM_VOIE"], True);
var intersectLayer = Intersects(township, $feature);
var cnt = Count(intersectLayer);
if (cnt > 0) {
    var feat = First(intersectLayer);
    var nomvoie = feat["NOM_VOIE"];
    if (IsEmpty(nomvoie)) {
        return "no NOM VOIE"; // or perhaps return null
    } else {
        var words = Split(nomvoie, " ");
        var cnt2 = Count(words);
        var lastword = words[cnt2-1];
        if (cnt2 > 1) {
            var otherwords = [];
            for (var i = 0; i < cnt2-1; i++) {
                otherwords[Count(otherwords)] = words[i];
            }
            return lastword + " (" +  Concatenate(otherwords, " ") + ")";
            
        } else {
            return lastword;
        }
    }    
} else {
    return "No township found"; // or perhaps return null
}

I tested with this code:

// RUE GEORGES BONNAC would be BONNAC (RUE GEORGES)
var nomvoie = "RUE GEORGES BONNAC";

if (IsEmpty(nomvoie)) {
    return "no NOM VOIE"; // or perhaps return null
} else {
    var words = Split(nomvoie, " ");
    Console(words);
    var cnt2 = Count(words);
    var lastword = words[cnt2-1];
    if (cnt2 > 1) {
        var otherwords = [];
        for (var i = 0; i < cnt2-1; i++) {
            otherwords[Count(otherwords)] = words[i];
        }
        return lastword + " (" +  Concatenate(otherwords, " ") + ")";
        
    } else {
        return lastword;
    }
}

... which returned this:

BONNAC (RUE GEORGES)

View solution in original post

XanderBakker
Esri Esteemed Contributor

Hi kbm_admin ,

Sure. Can you try this?

var township = FeatureSetByName($datastore, "GDB.SIGWRITER.FV_TRONC_L", ["NOM_VOIE"], True);
var intersectLayer = Intersects(township, $feature);
var cnt = Count(intersectLayer);
if (cnt > 0) {
    var feat = First(intersectLayer);
    var nomvoie = feat["NOM_VOIE"];
    if (IsEmpty(nomvoie)) {
        return "no NOM VOIE"; // or perhaps return null
    } else {
        var words = Split(nomvoie, " ");
        Console(words);
        var cnt2 = Count(words);
        var lastword = words[cnt2-1];
        var add = "";
        Console(Find("'", lastword));
        if (Find("'", lastword) > -1) {
            var arr = Split(lastword, "'");
            add = " " + arr[0] + "'";
            lastword = arr[1];
        }
        if (cnt2 > 1) {
            var otherwords = [];
            for (var i = 0; i < cnt2-1; i++) {
                otherwords[Count(otherwords)] = words[i];
            }
            return lastword + " (" +  Concatenate(otherwords, " ") + add + ")";
        } else {
            return lastword;
        }
    }
} else {
    return "No township found"; // or perhaps return null
}

View solution in original post

0 Kudos
8 Replies
XanderBakker
Esri Esteemed Contributor

Hi kbm_admin ,

There are a couple of things that may be going wrong here:

  • In you code example you don't return anything
  • The error might be related to how you read the field value from the feature. When retrieving a value from a field that contains an underscore always use feature["Field_Name"] instead of feature.Field_Name
  • Also it might be possible that no features where found with the intersect and when you try to take the first feature from a collection that has no feature it might fail. So always do a count to see if you have anything and continue accordingly

Try this:

var township = FeatureSetByName($datastore, "GDB.SIGWRITER.FV_TRONC_L", ["NOM_VOIE"], True);
var intersectLayer = Intersects(township, $feature);
var cnt = Count(intersectLayer);
if (cnt > 0) {
    var feat = First(intersectLayer);
    var nomvoie = feat["NOM_VOIE"];
    if (IsEmpty(nomvoie)) {
        return "no NOM VOIE"; // or perhaps return null
    } else {
        var words = Split(nomvoie, " ");    
        // define what to return, let's say you want to first word
        return words[0];
    }
} else {
    return "No township found"; // or perhaps return null
}
0 Kudos
KARIMLABIDI
Occasional Contributor

HI Xander, thanks for your answer. My problem is that I want the last word of the address, return it on the First Place and concatenate the other part of the address to return it after the First place. I don't know if it's clear , i put an example :  RUE GEORGES BONNAC would be BONNAC (RUE GEORGES).

Thanks Xander

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi KARIM LABIDI , 

I think this could do the trick:

var township = FeatureSetByName($datastore, "GDB.SIGWRITER.FV_TRONC_L", ["NOM_VOIE"], True);
var intersectLayer = Intersects(township, $feature);
var cnt = Count(intersectLayer);
if (cnt > 0) {
    var feat = First(intersectLayer);
    var nomvoie = feat["NOM_VOIE"];
    if (IsEmpty(nomvoie)) {
        return "no NOM VOIE"; // or perhaps return null
    } else {
        var words = Split(nomvoie, " ");
        var cnt2 = Count(words);
        var lastword = words[cnt2-1];
        if (cnt2 > 1) {
            var otherwords = [];
            for (var i = 0; i < cnt2-1; i++) {
                otherwords[Count(otherwords)] = words[i];
            }
            return lastword + " (" +  Concatenate(otherwords, " ") + ")";
            
        } else {
            return lastword;
        }
    }    
} else {
    return "No township found"; // or perhaps return null
}

I tested with this code:

// RUE GEORGES BONNAC would be BONNAC (RUE GEORGES)
var nomvoie = "RUE GEORGES BONNAC";

if (IsEmpty(nomvoie)) {
    return "no NOM VOIE"; // or perhaps return null
} else {
    var words = Split(nomvoie, " ");
    Console(words);
    var cnt2 = Count(words);
    var lastword = words[cnt2-1];
    if (cnt2 > 1) {
        var otherwords = [];
        for (var i = 0; i < cnt2-1; i++) {
            otherwords[Count(otherwords)] = words[i];
        }
        return lastword + " (" +  Concatenate(otherwords, " ") + ")";
        
    } else {
        return lastword;
    }
}

... which returned this:

BONNAC (RUE GEORGES)
KARIMLABIDI
Occasional Contributor

Yeah Xander it works perfectly. Thank you so much!

XanderBakker
Esri Esteemed Contributor

Hi KARIM LABIDI ,

Glad to hear that it works. Could you mark the post as the correct answer? If you need any additional explanation just let me now. 

0 Kudos
KARIMLABIDI
Occasional Contributor

Hi Xander,

do you know if it possible to add an element in the split? Sometimes , my strings are like this : AVENUE D'ARES so I need to keep ARES and not D'ARES. I would like find at the end :ARES (AVENUE D'). Do you know if it's possible to have this? Maybe with a replace for the ' by a " " but i'm not sure.

If you have a idea about this problem, I will be grateful.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi kbm_admin ,

Sure. Can you try this?

var township = FeatureSetByName($datastore, "GDB.SIGWRITER.FV_TRONC_L", ["NOM_VOIE"], True);
var intersectLayer = Intersects(township, $feature);
var cnt = Count(intersectLayer);
if (cnt > 0) {
    var feat = First(intersectLayer);
    var nomvoie = feat["NOM_VOIE"];
    if (IsEmpty(nomvoie)) {
        return "no NOM VOIE"; // or perhaps return null
    } else {
        var words = Split(nomvoie, " ");
        Console(words);
        var cnt2 = Count(words);
        var lastword = words[cnt2-1];
        var add = "";
        Console(Find("'", lastword));
        if (Find("'", lastword) > -1) {
            var arr = Split(lastword, "'");
            add = " " + arr[0] + "'";
            lastword = arr[1];
        }
        if (cnt2 > 1) {
            var otherwords = [];
            for (var i = 0; i < cnt2-1; i++) {
                otherwords[Count(otherwords)] = words[i];
            }
            return lastword + " (" +  Concatenate(otherwords, " ") + add + ")";
        } else {
            return lastword;
        }
    }
} else {
    return "No township found"; // or perhaps return null
}
0 Kudos
KARIMLABIDI
Occasional Contributor

Hi @XanderBakker,

yeah it works perfectly. Thank you so much. I very appreciate your help and your reactivity.

Thank a lot

0 Kudos