GROOVY EXERCISE
Exercise 1 – Adding new columns and removing rows.
> For this exercise, we want to do the following completely in Groovy.
• Run the "list addresses" MOCA command to build a result set.
• Remove any rows from the result set where the receiving phone number
(phnnum ) and receiving fax number ( faxnum )fields are BOTH null.
• For the remaining rows in the result set:
> Add a new column called phn_are_cd and fax_are_cd.
> Parse the area code from the phnnum and faxnum fields and load phn_are_cd
and fax_are_cd with the values.
> Note: parse based on the dash – do not assume that the first 3 digits are always
the area code.
> Modify the existing values for phnum and faxnum in the result set to they no
longer have the area code or the dash following the area code.
> Do not forget to check if ONE of the values
One possible solution:
[[
import com.redprairie.moca.*;
res = new SimpleResults();
cmd = "list addresses";
res = moca.executeInline(cmd);
res.addColumn("phn_are_cd", MocaType.STRING);
res.addColumn("fax_are_cd", MocaType.STRING);
while ( res.next() ) {
if ( res.isNull("phnnum") && res.isNull("faxnum") ) {
res.removeRow();
continue;
}
> Possible solution continued...
if ( !res.isNull("phnnum") ) {
def dash_index = res.getString("phnnum").indexOf("-");
def temp_are_cd = res.getString("phnnum").substring(0,dash_index);
def temp_number = res.getString("phnnum").substring(dash_index + 1);
res.setStringValue( "phn_are_cd", temp_are_cd );
res.setStringValue( "phnnum", temp_number );
}
if ( !res.isNull("faxnum") ) {
def dash_index = res.getString("faxnum").indexOf("-");
def temp_are_cd = res.getString("faxnum").substring(0,dash_index);
def temp_number = res.getString("faxnum").substring(dash_index + 1);
res.setStringValue( "fax_are_cd", temp_are_cd );
res.setStringValue( "faxnum", temp_number );
}
}
res;
]]
Comments
Post a Comment