Conditionally strip last few characters

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
mightycpa
Automation Wizard
Posts: 343
Joined: Mon Jan 12, 2004 4:07 pm
Location: Vienna, VA

Conditionally strip last few characters

Post by mightycpa » Tue Mar 10, 2015 10:17 pm

Hi,

I've got this working, but I'm wondering if there is a more elegant, simpler way to do it.

I have a script that gathers table names from SQL scripts. Sometimes, my script will pick up a fully qualified column name, and so I want to strip off the column name, leaving just the table name. For my tables and columns, the naming convention looks like this:

SCHEMA.OWNER.SOME_TABLE_NAME.SOME_COLUMN

Given that, I would want to yield this:

SCHEMA.OWNER.SOME_TABLE_NAME

Here's how I've done that:

I notice that in my naming convention, if a period follows an underscore, then that period marks the beginning of a column name. I parse through the string in order to find that condition. When I do, I SEPARATE the string using a period as my delimiter, then I just put all but the last item in the array back together, then strip off the final period, like so:

Code: Select all

Let>myvar=SCHEMA.OWNER.SOME_TABLE_NAME.SOME_COLUMN
Let>newstr=
RegEx>[^a-zA-Z0-9 -],myvar,0,m,nm,1,,mynewvar
Repeat>nm
  If>m_%nm%=.
    Let>v_pd_flag=1
  Endif
  If>m_%nm%=_
    If>v_pd_flag=1
      Separate>myvar,.,v_parts
      Let>j=1
      Repeat>j
        Concat>newstr,v_parts_%j%
        Concat>newstr,.
        Add>j,1
      Until>j=v_parts_count
      Let>nm=1
    EndIf
  EndIf
  Add>nm,-1
Until>nm<2

Let>newstr2={copy(%newstr%,1,length(%newstr%)-1)}
MessageModal>newstr2
But that sure does seem to be a lot of code, and I just wonder if there is a better way. Any ideas anybody?

Thanks in advance.
"A facility for quotation covers the absence of original thought." - Lord Peter Wimsey

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Conditionally strip last few characters

Post by Marcus Tettmar » Wed Mar 11, 2015 4:18 pm

Try this cunning method:

Code: Select all

Let>str=schema_owner.some_table_name.some_column
ExtractFileExt>str,last_bit
StringReplace>str,last_bit,,str
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

mightycpa
Automation Wizard
Posts: 343
Joined: Mon Jan 12, 2004 4:07 pm
Location: Vienna, VA

Re: Conditionally strip last few characters

Post by mightycpa » Wed Mar 11, 2015 6:04 pm

That's really nice Marcus. Cunning indeed! :idea:

My headline may have been a little misleading, there are actually two parts to my code:

1) Does the string contain a column name or not?

2) If so, strip off the last few characters.

So, to do the whole shooting match:

Code: Select all

Let>myvar=SCHEMA.OWNER.SOME_TABLE_NAME.SOME_COLUMN
RegEx>[^a-zA-Z0-9 -],myvar,0,m,nm,1,,mynewvar
Repeat>nm
  If>m_%nm%=.
    Let>v_pd_flag=1
  Endif
  If>m_%nm%=_
    If>v_pd_flag=1   //if a period exists to the right of an underscore
       ExtractFileExt>myvar,last_bit
       StringReplace>myvar,last_bit,,myvar
       Let>nm=1
    EndIf
  Endif
  Add>nm,-1
Until>nm<2
MessageModal>myvar
Two questions: Is there any danger in doing this?

StringReplace>myvar,last_bit,,myvar

Can you think of a similar cunning shortcut to answer the first question?

I really do like your solution... very "out of the box". Thanks
"A facility for quotation covers the absence of original thought." - Lord Peter Wimsey

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Conditionally strip last few characters

Post by Marcus Tettmar » Thu Mar 12, 2015 11:02 am

Ok. Try this:

Code: Select all

Let>str=first_bit.second_bit.third_bit

//is there a match of "_" followed by something followed by "."
RegEx>_.*\.,str,0,matches,nm,0
If>nm>0
  //yes - so remove the last bit that starts with a "."
  RegEx>\.([^\.]*)$,str,0,matches,nm,1,,str
Endif
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

mightycpa
Automation Wizard
Posts: 343
Joined: Mon Jan 12, 2004 4:07 pm
Location: Vienna, VA

Re: Conditionally strip last few characters

Post by mightycpa » Thu Mar 12, 2015 2:05 pm

Your solution:

RegEx>_.*\.,str,0,matches,nm,0

Here's the scary part: I actually recognized what that meant the moment I saw it.

Thanks Marcus.

I still have that one question however, for other purposes.

Is there any danger in doing this?

StringReplace>myvar,last_bit,,myvar
"A facility for quotation covers the absence of original thought." - Lord Peter Wimsey

mightycpa
Automation Wizard
Posts: 343
Joined: Mon Jan 12, 2004 4:07 pm
Location: Vienna, VA

Re: Conditionally strip last few characters

Post by mightycpa » Fri Mar 13, 2015 4:18 pm

RegEx>\.([^\.]*)$,str,0,matches,nm,1,,str

Never mind. Got it.
"A facility for quotation covers the absence of original thought." - Lord Peter Wimsey

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts