Jump to content
Sign in to follow this  
EphratahNothing

Changing Variable Reference in Script by Using the Script Itself

Recommended Posts

EphratahNothing

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.

Share this post


Link to post
Share on other sites
water

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 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
EphratahNothing

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?

Share this post


Link to post
Share on other sites
water

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 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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  

×