Sign in to follow this  
Followers 0
skippynz

help with script cleanup - regular expressions etc

2 posts in this topic

Ive got my script working ok but want to tidy it up a bit - ive been told about regular expressions but not sure where to start - can someone help me with this ?

TIA

Craig

CODE

_DateLongToShort('Sunday, 14 June 2009)

#Region - LONGDATE FORMAT TO SHORTDATE

Func _DateLongToShort($LONG_DATE_FORMAT)

Select

Case StringInStr($LONG_DATE_FORMAT, 'January')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' January ', ',01,')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ',', '/')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' ', '')

$SHORT_DATE_FORMAT = StringRight($LONG_DATE_FORMAT, 10)

Case StringInStr($LONG_DATE_FORMAT, 'February')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' February ', ',02,')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ',', '/')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' ', '')

$SHORT_DATE_FORMAT = StringRight($LONG_DATE_FORMAT, 10)

Case StringInStr($LONG_DATE_FORMAT, 'March')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' March ', ',03,')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ',', '/')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' ', '')

$SHORT_DATE_FORMAT = StringRight($LONG_DATE_FORMAT, 10)

Case StringInStr($LONG_DATE_FORMAT, 'April')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' April ', ',04,')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ',', '/')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' ', '')

$SHORT_DATE_FORMAT = StringRight($LONG_DATE_FORMAT, 10)

Case StringInStr($LONG_DATE_FORMAT, 'May')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' May ', ',05,')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ',', '/')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' ', '')

$SHORT_DATE_FORMAT = StringRight($LONG_DATE_FORMAT, 10)

Case StringInStr($LONG_DATE_FORMAT, 'June')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' June ', ',06,')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ',', '/')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' ', '')

$SHORT_DATE_FORMAT = StringRight($LONG_DATE_FORMAT, 10)

Case StringInStr($LONG_DATE_FORMAT, 'July')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' July ', ',07,')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ',', '/')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' ', '')

$SHORT_DATE_FORMAT = StringRight($LONG_DATE_FORMAT, 10)

Case StringInStr($LONG_DATE_FORMAT, 'August')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' August ', ',08,')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ',', '/')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' ', '')

$SHORT_DATE_FORMAT = StringRight($LONG_DATE_FORMAT, 10)

Case StringInStr($LONG_DATE_FORMAT, 'September')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' September ', ',09,')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ',', '/')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' ', '')

$SHORT_DATE_FORMAT = StringRight($LONG_DATE_FORMAT, 10)

Case StringInStr($LONG_DATE_FORMAT, 'October')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' October ', ',10,')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ',', '/')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' ', '')

$SHORT_DATE_FORMAT = StringRight($LONG_DATE_FORMAT, 10)

Case StringInStr($LONG_DATE_FORMAT, 'November')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' November ', ',11,')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ',', '/')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' ', '')

$SHORT_DATE_FORMAT = StringRight($LONG_DATE_FORMAT, 10)

Case StringInStr($LONG_DATE_FORMAT, 'December')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' December ', ',12,')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ',', '/')

$LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' ', '')

$SHORT_DATE_FORMAT = StringRight($LONG_DATE_FORMAT, 10)

EndSelect

If StringLeft($SHORT_DATE_FORMAT, 1) = '/' Then

$SHORT_DATE_FORMAT = StringTrimLeft($SHORT_DATE_FORMAT, 1)

EndIf

Return ($SHORT_DATE_FORMAT)

EndFunc ;==>_DateLongToShort

Share this post


Link to post
Share on other sites



sorry for the delay in replying - been a bit hectic - yep you were right about declaring the vars earlier - thanks for the code below that helps to tidy up my script nicely.

First of all, you left your quotes open in Line 1. Second, you haven't declared any of your variables before using them, but I suppose you may have done that in an unposted portion of the script. Lastly, you should use Scite's Tidy function. Open your script in Scite and press "Ctrl+T", or select "Tidy" from the "Tools" menu.

I don't know RegEx so I can't help you with that. I can say that if this was my script I would've used two arrays for the months and do the StringReplace in a For...Next Loop to avoid the large Select...Case.

EDIT: Here is the code I had in mind. I added a Message Box to make sure it worked. I also declared the variables in the first line so the Message Box would work.

Local $LONG_DATE_FORMAT, $SHORT_DATE_FORMAT

_DateLongToShort('Sunday, 14 June 2009')

MsgBox(0, "Test", $SHORT_DATE_FORMAT)

Func _DateLongToShort($LONG_DATE_FORMAT)
    Local $array1[12] = ['January ', 'February ', 'March ', 'April ', 'May ', 'June ', 'July ', 'August ', 'September ', 'October ', 'November ', 'December ']
    Local $array2[12] = [',01,', ',02,', ',03,', ',04,', ',05,', ',06,', ',07,', ',08,', ',09,', ',10,', ',11,', ',12,']
    For $X = 0 To 11
        $LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, $array1[$X], $array2[$X])
    Next
    $LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ',', '/')
    $LONG_DATE_FORMAT = StringReplace($LONG_DATE_FORMAT, ' ', '')
    $SHORT_DATE_FORMAT = StringRight($LONG_DATE_FORMAT, 10)
    If StringLeft($SHORT_DATE_FORMAT, 1) = '/' Then $SHORT_DATE_FORMAT = StringTrimLeft($SHORT_DATE_FORMAT, 1)
    Return ($SHORT_DATE_FORMAT)
EndFunc   ;==>_DateLongToShort

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0