Prepare For Output--Parse Zipcode
The following code is used to split zip codes from a pattern match. The code below takes a zip code and assigns the first five digits to the variable "ZIP". If the zip code is in the longer format (12345-6789), as opposed to the shorter format (12345), then the second part of the zip code, which comes after the "-" character, is assigned to the "ZIP4" variable (so named for the 4 digits following the "-" character). This script would be useful in cases where zip codes must be standardized.
try{
// Local reference to variables
String zip = dataRecord.get("ZIP");
if(zip != null){
// Split the zip code on the "-" character (for zip codes in the 12345-6789 format)
String[] zipParts = zip.split("-");
// Put parts in dataRecord
dataRecord.put("ZIP", zipParts[0]);
// If we were able to split the zip into two pieces (for zip codes in the 12345-6789 format),
// then we store the last four digits in the variable "ZIP4"
if (zipParts.length == 2){
dataRecord.put("ZIP4", zipParts[1]);
}
}
}
catch(Exception e){
session.log("Error running Fix Zip Codes and Nulls");
}