List Processing Coding Tip

I didn’t realize you could do that. Just surround it with code blocks.

Thanks for the tip.

I’m not sure if this entirely obvious, but I saw it in code once and like it enough that I thought I would pass it on.

When you need to compare two consecutive items in a list, you need to compare the current with the next. But, if you compare to the next and are at the end of the list, the next is the first. This means an If statement to check if you should compare to 0 or not when at the end of the list.

lPreviousItem = lItemCount-1
For lItem = 0 to lItemCount-1
‘special case for end of list
If lItem = lItemCount-1 then
‘compare ‘lItem’ with ‘0’
Else
‘compare ‘lItem’ with ‘lItem+1’
End If
Next lItem

But, if you compare to the previous (always possible, I think) this is an option

lPreviousItem = lItemCount-1
For lItem = 0 to lItemCount-1
compare ‘lPreviousItem’ with ‘lItem’
lPreviousItem = lItem
Next lItem

I don’t know what this translates to in machine code, but from a clarity perspective, this is much nicer, in my opinion.

Darn, that’s not very readable.

Wow… I tried. there is no way with this forum to make that look pretty… <!– s:-( –><img src="{SMILIES_PATH}/icon_e_sad.gif" alt=":-(" title="Sad" /><!– s:-( –>

[pre class=’ip-ubbcode-code-pre’]
lPreviousItem = lItemCount-1

For lItem = 0 to lItemCount-1
‘special case for end of list
If lItem = lItemCount-1 then
‘compare ‘lItem’ with ‘0’
Else
‘compare ‘lItem’ with ‘lItem+1’
End If
Next lItem
[/pre]

[pre class=’ip-ubbcode-code-pre’]
lPreviousItem = lItemCount-1

For lItem = 0 to lItemCount-1
compare ‘lPreviousItem’ with ‘lItem’
lPreviousItem = lItem
Next lItem
[/pre]

You must be logged in to reply in this thread.

5 posts
recent posts