Finding a "whole string" within a string with Index (or another way)? - openedge

I am reading in a list of constituent attributes (tags) and only want to keep a subset of them. Here is how I attempted to do that.
DEFINE VARIABLE tagsToKeep AS CHARACTER.
tagsToKeep = "Business Contact,Celebrate 2015,Celebrate 2017,Celebrate 2019,Certificate - Former Holder,Non-MB Church".
/*do a bunch of stuff to get the cRecord which is one tag, often with spaces or other characters in it.*/
IF INDEX(tagsToKeep, cRecord) > 0 THEN
DO:
CREATE ttTag.
ASSIGN
ttTag.tag-code = cRecord
ttTag.constituent-id = ttAccount.pin-string.
ttTag.tag-name = cRecord.
END.
The trouble is that one of the tags is "Certificate" which is a substring of one of the strings in the TagsToKeep string -- "Certificate - Former Holder" gets included and created as a Tag. I only want to match on the strings that are essentially "whole word only".
Is there a (better) way to do what I want to do?

In your code, use LOOKUP instead of INDEX
IF LOOKUP (cRecord, tagsToKeep) > 0 THEN
(comma-) delimited lists are a key concept in the ABL.

If cRecord is a single tag, then it may be useful to take the list of to-keep tags and put those tags into a separate temp-table
DEFINE VARIABLE loop AS INTEGER NO-UNDO.
DEFINE VARIABLE cnt AS INTEGER NO-UNDO.
// Define and populate temp-table
DEFINE TEMP-TABLE ttTagsToKeep NO-UNDO
FIELD TagName AS CHARACTER
INDEX idx1 AS PRIMARY UNIQUE TagName.
cnt = NUM-ENTRIES(cTagsToKeep).
DO loop = 1 to cnt:
CREATE ttTagsToKeep.
ASSIGN ttTagsToKeep.TagName = TRIM(ENTRY(loop, cTagsToKeep)).
END.
Now you can look for a matching record. Whether you clean up cRecord or do multiple CAN-FINDs is up to you.
IF CAN-FIND(ttTagsToKeep WHERE ttTagsToKeep.TagName EQ cRecord) THEN
DO:
// create ttTag record
END.

Related

While processing XML, how to insert a node after a specific node?

Consider the following XML document that I receive and parse:
<Courses>
<Course>
<Name>Intro to DB</Name>
<Credits>3.0</Credits>
</Course>
<Course>
<Name>Intro to Programming</Name>
<Credits>3.0</Credits>
</Course>
</Courses>
If I want to add a new course between two courses or a new element within each course (e.g. courseId). How can I achieve this in Progress? I've given a read to Progress' XML documentation (for DOM and SAX) and found INSERT-BEFORE(), not sure if that can be used to achieve this.
Do I have to use temp-table/ ProDataSets to achieve this?
a example with a temp-table and a dataset
the dataset here is only defined for formatting the xml output
DEFINE TEMP-TABLE ttCourse NO-UNDO XML-NODE-NAME "Course"
FIELD dSORT AS DECIMAL SERIALIZE-HIDDEN // hidden in xml output
FIELD Name AS CHARACTER
FIELD Credits AS DECIMAL
FIELD NEWFIELD AS CHARACTER
INDEX SORT dSORT ASCENDING. // this index for sorting the xml output Course
DEFINE VARIABLE cxml AS LONGCHAR NO-UNDO.
DEFINE VARIABLE iCnt AS INTEGER NO-UNDO.
// DEFINE DATASET only for xml-node-name
DEFINE DATASET ds
XML-NODE-NAME "Courses" FOR ttCourse.
cxml = "<Courses>
<Course>
<Name>Intro to DB</Name>
<Credits>3.0</Credits>
</Course>
<Course>
<Name>Intro to Programming</Name>
<Credits>3.0</Credits>
</Course>
</Courses>".
TEMP-TABLE ttCourse:READ-XML ("LONGCHAR", cxml, "MERGE", "", FALSE, ?, ?).
// for sorting output use field dsort
FOR EACH ttCourse WHERE ttCourse.DSORT = 0:
iCnt = iCnt + 1.
ttCourse.DSORT = iCnt.
END.
// insert a record in the middle
CREATE ttCourse.
ASSIGN
ttCourse.DSORT = 1.5
ttCourse.name = "test"
ttCourse.credits = 5.5
ttCourse.NEWFIELD = "NEWFIELD"
.
DATASET ds:WRITE-XML ("file", "C:/temp/baz/courses.xml", TRUE,?,?,FALSE,FALSE).

How to access controls?

I have just created a window, containing a fill-in field (TestField) and a checkbox (chk_TestField):
DEFINE VARIABLE TestField AS INTEGER VIEW-AS FILL-IN.
DEFINE VARIABLE chk_TestField AS LOGICAL VIEW_AS TOGGLE-BOX.
It is very simple to change the value of the fill-in field, based on the checking of the checkbox, something like:
ON VALUE-CHANGED OF chk_TestField IN FRAME DEFAULT-FRAME
DO:
TestField = 5.
END.
However, I'm interested in changing an attribute of the Fill-in field itself, not the integer it represents (I would like to make the fill-in field read-only), how to I do this?
I've tried:
ON VALUE-CHANGED OF chk_TestField IN FRAME DEFAULT-FRAME
DO:
TestField.Read-Only = NOT(chk_TestField).
END.
This, obviously, does not work.
ASSIGN TestField:READ-ONLY IN FRAME {&FRAME-NAME} = TRUE.
or
ASSIGN Table.TestField:READ-ONLY IN FRAME {&FRAME-NAME} = TRUE.

Update dictionary key inside list using map function -Python

I have a dictionary of phone numbers where number is Key and country is value. I want to update the key and add country code based on value country. I tried to use the map function for this:
print('**Exmaple: Update phone book to add Country code using map function** ')
user=[{'952-201-3787':'US'},{'952-201-5984':'US'},{'9871299':'BD'},{'01632 960513':'UK'}]
#A function that takes a dictionary as arg, not list. List is the outer part
def add_Country_Code(aDict):
for k,v in aDict.items():
if(v == 'US'):
aDict[( '1+'+k)]=aDict.pop(k)
if(v == 'UK'):
aDict[( '044+'+k)]=aDict.pop(k)
if (v == 'BD'):
aDict[('001+'+k)] =aDict.pop(k)
return aDict
new_user=list(map(add_Country_Code,user))
print(new_user)
This works partially when I run, output below :
[{'1+952-201-3787': 'US'}, {'1+1+1+952-201-5984': 'US'}, {'001+9871299': 'BD'}, {'044+01632 960513': 'UK'}]
Notice the 2nd US number has 2 additional 1s'. What is causing that?How to fix? Thanks a lot.
Issue
You are mutating a dict while iterating it. Don't do this. The Pythonic convention would be:
Make a new_dict = {}
While iterating the input a_dict, assign new items to new_dict.
Return the new_dict
IOW, create new things, rather than change old things - likely the source of your woes.
Some notes
Use lowercase with underscores when defining variable names (see PEP 8).
Lookup values rather than change the input dict, e.g. a_dict[k] vs. a_dict.pop(k)
Indent the correct number of spaces (see PEP 8)

In Biztalk mapper how to use split array concept

Required suggestion on below part.please any one give solution.
We have mapping from 850 to FlatFile
X12/PO1Loop1/PO1/PO109 and I need to map to field VALUE which is under record Option which is unbounded.
Split PO109 into substrings delimited by '.', foreach subsring after the first, create new Option with value=substring
So in input sample we have value like 147895632qwerqtyuui.789456123321456987
Similarly the field repeats under POLoop1.
So I need to split value based on (.) then pass a value to value field under option record(unbounded).
I tried using below code snippet
public string SplitValues(string strValue)
{
string[] arrValue = strValue.Split(".".ToCharArray());
foreach (string strDisplay in arrValue)
{
return strDisplay;
}
}
But it doesn't works, and I am not really familiar with the String methods and I am not sure if there's an easy way to do this. I have a String which contains couple of values delimited with "." .
So I need to separate values based on delimiter(.) and pass value to field.
How can I do this
As I mentioned, not too clear what is your objective, but I think you want to split a node that has some kind of delimiter into multiple nodes... if so, Try this: https://seroter.wordpress.com/2008/10/07/splitting-delimited-values-in-biztalk-maps/
He is doing exactly that. Given a node with a|b|c|d as value, output multiple nodes, each containing the value after splitted by |, so node1 = a, node2 = b, node3 = c, node4 = d.

Substring with Multiple Arguments

I have a dropdownlist that has the value of two columns in it... One column is a number ranging from 5 characters long to 8 characters long then a space then the '|' character and another space, followed by a Description for the set of numbers.
An example:
12345678 | Description of Product
In order to pull the items for the dropdownlist into my database I need a to utilize a substring to pull the sequence of numbers out only.
Is it possible to write a substring to pull multiple character lengths? (Sometimes it may be 6 numbers, sometimes 5 numbers, sometimes 8, it would depend on what the user selected from the dropdownlist.)
Use a regular expression for this.
Assuming the number is at the start of the string, you can use the following:
^[0-9]+
Usage:
var theNumbers = RegEx.Match(myDropdownValue, "^[0-9]+").Value;
You could also use string.Split to get the parts separated by | if you know the first part is what you need and will always be numeric:
var theNumbers = myDropdownValue.Split("| ".ToCharArray(),
StringSplitOptions.RemoveEmptyEntries)[0];
Either of these approaches will result in a string. You can use int.Parse on the result in order to get an integer from it.
This is how I would do it
string str = "12345678 | Description of Product";
int delimiter;
delimiter = str.IndexOf("|") - 1;
string ID =str.substring(0, delimiter);
string desc = str.substring(delimiter + 1, str.length - 1);
Try using a regex to pull out the first match of a sequence of numbers of any length. The regex will look something like "^\d+" - starts with any number of decimal digits.
Instead of using substring, you should use Split function.
var words = phrase.Split(new string[] {" | "},
StringSplitOptions.RemoveEmptyEntries);
var number = word[0];

Resources