Jump to content

# How Do I Use "If Then" More Efficiently?

## Recommended Posts

I am using a repetitive if then statements and I know there has to be a better of doing this.

Is it possible to to do this without writing seperate statments for each line?

```;line 1
If IsArray(\$line1vars) And \$line1vars[4] <> 0 Then
\$code1 = \$line1vars[4]
Else
\$code1 = 0
EndIf
;line2
If \$total >= 2 Then
Local \$line2 = \$vars[2]
\$line2vars = StringSplit(\$line2, @TAB)
EndIf
If IsArray(\$line2vars) And \$line2vars[4] <> 0 Then
\$code2 = \$line2vars[4]
Else
\$code2 = 0
EndIf
;line 3
If \$total >= 3 Then
Local \$line3 = \$vars[3]
\$line3vars = StringSplit(\$line3, @TAB)
EndIf
If IsArray(\$line3vars) And \$line3vars[4] <> 0 Then
\$code3 = \$line3vars[4]
Else
\$code3 = 0
EndIf
;line 4
If \$total >= 4 Then
Local \$line4 = \$vars[4]
\$line4vars = StringSplit(\$line4, @TAB)
EndIf
If IsArray(\$line4vars) And \$line4vars[4] <> 0 Then
\$code4 = \$line4vars[4]
Else
\$code4 = 0
EndIf
;line 5
If \$total >= 5 Then
Local \$line5 = \$vars[5]
\$line5vars = StringSplit(\$line5, @TAB)
EndIf
If IsArray(\$line5vars) And \$line5vars[4] <> 0 Then
\$code5 = \$line5vars[4]
Else
\$code5 = 0
EndIf
;line 6
If \$total >= 6 Then
Local \$Line6 = \$vars[6]
;MsgBox(0, "line6", \$line6)
\$line6vars = StringSplit(\$Line6, @TAB)
EndIf
If IsArray(\$line6vars) And \$line6vars[4] <> 0 Then
\$code6 = \$line6vars[4]
Else
\$code6 = 0
EndIf
;line 7
If \$total >= 7 Then
Local \$line7 = \$vars[7]
\$line7vars = StringSplit(\$line7, @TAB)
EndIf
If IsArray(\$line7vars) And \$line7vars[4] <> 0 Then
\$code7 = \$line7vars[4]
Else
\$code7 = 0
EndIf
;line 8
If \$total >= 8 Then
Local \$line8 = \$vars[8]
\$line8vars = StringSplit(\$line8, @TAB)
EndIf
If IsArray(\$line8vars) And \$line8vars[4] <> 0 Then
\$code8 = \$line8vars[4]
Else
\$code8 = 0
EndIf
;line 9
If \$total >= 9 Then
Local \$line9 = \$vars[9]
\$line9vars = StringSplit(\$line9, @TAB)
EndIf
If IsArray(\$line9vars) And \$line9vars[4] <> 0 Then
\$code9 = \$line9vars[4]
Else
\$code9 = 0
EndIf
;line10
If \$total >= 10 Then
Local \$line10 = \$vars[10]
\$line10vars = StringSplit(\$line10, @TAB)
EndIf
If IsArray(\$line10vars) And \$line10vars[4] <> 0 Then
\$code10 = \$line10vars[4]
Else
\$code10 = 0
EndIf
;line11
If \$total >= 11 Then
Local \$line11 = \$vars[11]
\$line11vars = StringSplit(\$line11, @TAB)
EndIf
If IsArray(\$line11vars) And \$line11vars[4] <> 0 Then
\$code11 = \$line11vars[4]
Else
\$code11 = 0
EndIf
;line12
If \$total >= 12 Then
Local \$line12 = \$vars[12]
\$line12vars = StringSplit(\$line12, @TAB)
EndIf
If IsArray(\$line12vars) And \$line12vars[4] <> 0 Then
\$code12 = \$line12vars[4]
Else
\$code12 = 0
;so on and so forth......
EndIf```

As you can see it is very repetitive. I do not have a concrete ceiling on how high it may need to go. Does anyone have any suggestions on a better way to do this.

Edited by mithandir1

#### Share this post

##### Share on other sites

Hi mithandir1,

first thin, you could use an array as result for your \$code1 ... \$coden variables,

then you could create a loop and use eval()

Example:

```\$var1 = 2
\$var2 = 4
\$var3 = 8

For \$i = 1 To 3
ConsoleWrite(Eval("var" & \$i) & @CRLF)
Next```

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

#### Share this post

##### Share on other sites

Okay,

obviously it can be really easy:

```Dim \$a_codes[\$total + 1]
For \$i = 1 To \$total
\$a_temp = 0
\$a_temp = StringSplit(\$vars[\$i], @TAB)
If IsArray(\$a_temp) And \$a_temp[4] <> 0 Then
\$a_codes[\$i] = \$a_temp[4]
Else
\$a_codes[\$i] = 0
Next
Next```

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

## 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

×

• Wiki

• Back

• Git