Jump to content

Changing Variable Reference in Script by Using the Script Itself


Recommended Posts

First post ever.

I am trying to parse a large text file which has a sections that look like this:

1 ACD 0002500008 INCALLS VZLABB 1 Y 00008

4 DN 0002500009

10 MDN 0002500520 MCA PRIM:N RING :ALWAYS NCOS:3

11 MDN 0002500521 MCA PRIM:N RING :ALWAYS NCOS:3

12 MDN 0002500524 MCA PRIM:N RING :ALWAYS NCOS:3

by using code that looks like this:

If StringStripWS(StringLeft($Line, 6), 8) = "1" Then

$KEY_DN_1 = $KEY_DN_1 & StringStripWS(StringTrimLeft($Line,StringInStr($Line," ")),1)

ElseIf StringStripWS(StringLeft($Line, 6), 8) = "2" Then

$KEY_DN_2 = $KEY_DN_2 & StringStripWS(StringTrimLeft($Line,StringInStr($Line," ")),1)

ElseIf StringStripWS(StringLeft($Line, 6), 8) = "3" Then

$KEY_DN_3 = $KEY_DN_3 & StringStripWS(StringTrimLeft($Line,StringInStr($Line," ")),1)

ElseIf StringStripWS(StringLeft($Line, 6), 8) = "4" Then

$KEY_DN_4 = $KEY_DN_4 & StringStripWS(StringTrimLeft($Line,StringInStr($Line," ")),1)

ElseIf StringStripWS(StringLeft($Line, 6), 8) = "5" Then

$KEY_DN_5 = $KEY_DN_5 & StringStripWS(StringTrimLeft($Line,StringInStr($Line," ")),1)

... and so on until ...

ElseIf StringStripWS(StringLeft($Line, 6), 8) = "64" Then

$KEY_DN_64 = $KEY_DN_64 & StringStripWS(StringTrimLeft($Line,StringInStr($Line," ")),1)

Endif

I am thinking, rather than writing 128 lines of code that there must be a more elegant way of doing this.

I picture a loop that increments from 1 to 64 and changes the names of the equivicant and the names of the variables.

Unfortunately I can discover no way to change the names of the variable on each pass through the loop.

Would somebody tell me if this is possible and, if it is, steer me in the right direction, please?

TIA!

BTW, please ignore that my logic is flawed in that any line that starts with 1 could get get caught twice (1 and 10, for example). My question is not so much about what the lines of code are doing but rather how to avoid having to write so many of them.

Link to comment
Share on other sites

Don't use individual variables, use an array.

Global $KEY_DN[65] ; because an array starts with element 0. As the data starts with 1 element 0 will be empty
$iNumber = StringStripWS(StringLeft($Line, 6), 8)
$KEY_DN[$iNumber] = $KEY_DN[$iNumber] & StringStripWS(StringTrimLeft($Line,StringInStr($Line," ")),1)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Sweet!

Here is what I wound up with:

Global $KEY_DN_[65]
For $j = 1 To 64
If StringStripWS(StringLeft($Line, StringLen($j)), 8) = $j Then
$KEY_DN_[$j] = $KEY_DN_[$j] & StringStripWS(StringTrimLeft($Line, StringInStr($Line, " ")), 1)
EndIf
Next

This helped to to eliminate over 700 lines of code! (I had to do something like this numerous times.)

Thanks, Water!

Now, how do I mark this topic as SOLVED?

Link to comment
Share on other sites

Here is what I wound up with:

Global $KEY_DN_[65]
For $j = 1 To 64
If StringStripWS(StringLeft($Line, StringLen($j)), 8) = $j Then
$KEY_DN_[$j] = $KEY_DN_[$j] & StringStripWS(StringTrimLeft($Line, StringInStr($Line, " ")), 1)
EndIf
Next
You can even enhance performance of your script. The beauty of arrays is that you can access each element directly by its index. So you could use something like this:

Global $KEY_DN_[65]
Global $iNumber = StringStripWS(StringLeft($Line, StringLen($j)), 8)
If IsNumber($iNumber) And $iNumber >= 1 And $iNumber <= 64 Then _
    $KEY_DN_[$iNumber] = $KEY_DN_[$iNumber] & StringStripWS(StringTrimLeft($Line, StringInStr($Line, " ")), 1)

Now, how do I mark this topic as SOLVED?

You can't. I think you have to have at least 5 poste before you can change the title of your thread.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...