1

We have a department in the organization that was given a limited-length description field in a legacy application. They came up with their own acronyms and abbreviations for various phrases, which we would like to convert back to English phrases.

I'd like to have a better way to parse these descriptions than what I'm currently doing.

Sample Items:

Part #:         Description
-------------------------------------
10001           17OH 2P 2G TRK 2YR
10002           22OH 2P 3G TRK 1YR

In these sample descriptions, I need to parse these to pull the following items:

17OH = 17 Ohm
22OH = 22 Ohm
2P   = 2 Pole
2G   = 2-Gang
3G   = 3-Gang
TRK  = Track
2YR  = 2-Year Warranty
1YR  = 1-Year Warranty

The legacy application is written in VBScript/Classic ASP, and so the following would be an example of a way to convert these to English.

descriptionOUTPUT = ""
descriptionRAW = "17OH 2P 2G TRK 2YR"
if instr(descriptionRAW,"17OH") > 0 then descriptionOUTPUT = descriptionOUTPUT & "17 Ohm "
if instr(descriptionRAW,"22OH") > 0 then descriptionOUTPUT = descriptionOUTPUT & "22 Ohm "
if instr(descriptionRAW,"2P") > 0 then descriptionOUTPUT = descriptionOUTPUT & "2 Pole "
if instr(descriptionRAW,"2G") > 0 then descriptionOUTPUT = descriptionOUTPUT & "2 Gang "
if instr(descriptionRAW,"3G") > 0 then descriptionOUTPUT = descriptionOUTPUT & "3 Gang "
if instr(descriptionRAW,"TRK") > 0 then descriptionOUTPUT = descriptionOUTPUT & "Track "
if instr(descriptionRAW,"2YR") > 0 then descriptionOUTPUT = descriptionOUTPUT & "2-Year Warranty "
if instr(descriptionRAW,"1YR") > 0 then descriptionOUTPUT = descriptionOUTPUT & "1-Year Warranty "
response.write(descriptionOUTPUT)

The output would be:

17 Ohm 2 Pole 2 Gang Track 2-Year Warranty

This would be fine if we just had a dozen or so items to parse out, but we probably have hundreds of these and the repetition of the "if instr" statements are surely hugely inefficient even if we're only running that code a couple hundred times per day. Is there a better way to do this using an array or Regex, or anything more efficient than the above?

4
  • You should use a lookup table driven from a database, it would make this far easier to manage and reduce the code smell. Commented Sep 6, 2020 at 0:17
  • They will come from the database, I just didn't want to overcomplicate the question when the worry is more about the code utilized rather than the source of the items that needed to be looked up. Commented Sep 6, 2020 at 17:41
  • @Beems Consider using the Replace function. See: stackoverflow.com/a/14973169 Commented Sep 6, 2020 at 19:04
  • Look if they are coming from the database anyway, the code can be a one / two liner for every iteration was the point I was trying to make. Commented Sep 6, 2020 at 20:06

2 Answers 2

1

A very basic way would be to use the dictionary object in asp. Either hard code them or read them from database and compare in a loop.

https://www.w3schools.com/asp/asp_ref_dictionary.asp

Sign up to request clarification or add additional context in comments.

3 Comments

I will look into this, thanks. Can you link any good examples that are applicable to this scenario?
Why add a dictionary if you already have a recordset?
As I said either hard code them (like the original) OR read from a db.
-1

Bit late to answer this, but in case you're still looking, this is fairly straightforward using the replace command:

descriptionOUTPUT = replace(replace(replace(descriptionRAW,"OH"," ohm"),"P"," Pole"),"G","-Gang")
response.write(descriptionOUTPUT)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.