From: Larry Serflaten on

"Jim Mack" <jmack(a)mdxi.nospam.com> wrote

> Actually, if the data set won't often change during a run, then at
> runtime (or whenever) I might concatenate all the .symbols into one
> string, space-padded to a width of two in each section, and delimited
> by something like pipes:
>
> |ST|R |AB|C |GM| ... etc
>
> Scanning for "C" then is very fast:
>
> Posn = Instr(Search$, "|C ") \ 3 ' -1 if 0-based array
>
> Of course, if you don't search very often such optimizations are
> academic.

If I might add my 3 cents... I'd suggest allowing for 3 characters.

A long, long, long time ago (internet time...) consensus was that
two characters just wasn't enough. There was the possiblity of
too many collisions as well as limited quantity.

Ever notice just how many commands and functions are 3 characters
long (from back in the days when the OS, the interpreter, and your
program had to fit in 64K)?

Asc, Chr, Clr, Cos, Dim, For, Get, Let, Len, Mid, Mod, ... etc., etc...

LFS



From: Larry Serflaten on

"Jim Mack" <jmack(a)mdxi.nospam.com> wrote

> Posn = Instr(Search$, "|C ") \ 3 ' -1 if 0-based array

Shouldn't that be:

idx = InStr(Names, "|C ") \ 3 ' + 1 if 1-based array


LFS





From: Jim Mack on
Larry Serflaten wrote:
> "Jim Mack" wrote...
>
>> Posn = Instr(Search$, "|C ") \ 3 ' -1 if 0-based array
>
> Shouldn't that be:
>
> idx = InStr(Names, "|C ") \ 3 ' + 1 if 1-based array

Yes. The problem with off-the-cuff code...

Of course, we'd still have the issue of a not-found item. You probably
want to separate the InStr from the division by 3 -- or 4 (-: -- and
insert a test for 0.

--
Jim

From: Nobody on
"Webbiz" <nospam(a)noway.com> wrote in message
news:mad8p5lstridshvadvvfjmmenijgmijmsf(a)4ax.com...
> Type SYMBOLS
> ChartName as String
> Symbol as String
> End Type
>
> Public gSymbols() as SYMBOLS
>
> ...
>
> gSymbols(0).ChartName = "Soybeans"
> gSymbols(0).Symbol = "S"
>
> gSymbols(1).ChartName = "Apr Live Cattle 2009"
> gSymbols(1).Symbol = "LC"
>
> etc. etc.

This is very common, and simple to implement, especially if the array size
is fixed. Below is a dynamic array version that auto expands as needed when
adding more items. It's preferable that you put this in a class to make it
easy to manage your code.

Output:

gSymbolsCount = 5
Item4 is at 4
'D' is at 4
gSymbolsCount = 4
Item4 is at 0
'D' is at 0


' Form1 code

Option Explicit

Private Sub Form_Load()
SymbolsAdd "Item1", "A"
SymbolsAdd "Item2", "B"
SymbolsAdd "Item3", "C"
SymbolsAdd "Item4", "D"
SymbolsAdd "Item5", "E"

Debug.Print "gSymbolsCount = " & gSymbolsCount

Debug.Print "Item4 is at " & SymbolsFindChartName("Item4")
Debug.Print "'D' is at " & SymbolsFindSymbol("D")

SymbolsRemove 4

Debug.Print "gSymbolsCount = " & gSymbolsCount

Debug.Print "Item4 is at " & SymbolsFindChartName("Item4")
Debug.Print "'D' is at " & SymbolsFindSymbol("D")

End Sub


' Module1 code

Option Explicit

Type TSymbols
ChartName As String
Symbol As String
End Type

Public gSymbols() As TSymbols
Public gSymbolsCount As Long

' Returns True when item successfully added, False when out of memory
Public Function SymbolsAdd(ByRef ChartName As String, _
ByRef Symbol As String) As Boolean
Dim i As Long

On Error Resume Next

i = UBound(gSymbols)
If Err.Number <> 0 Then
' Not Redimmed before
Err.Clear
ReDim Preserve gSymbols(1 To 100)
If Err.Number <> 0 Then
' Out of memory
SymbolsAdd = False
Exit Function
End If
End If

If gSymbolsCount = UBound(gSymbols) Then
' Time to increase array size
ReDim Preserve gSymbols(1 To UBound(gSymbols) + 100)
If Err.Number <> 0 Then
' Out of memory
SymbolsAdd = False
Exit Function
End If
End If

' Add item to the array
gSymbolsCount = gSymbolsCount + 1
gSymbols(gSymbolsCount).ChartName = ChartName
gSymbols(gSymbolsCount).Symbol = Symbol

SymbolsAdd = True ' Success

End Function

Public Sub SymbolsRemove(ByVal Start As Long)
Dim i As Long

If Start >= 1 And Start <= gSymbolsCount Then
For i = Start To gSymbolsCount - 1
gSymbols(i) = gSymbols(i + 1)
Next
gSymbolsCount = gSymbolsCount - 1
End If
End Sub

' Returns the index in gSymbols() array, 0 if not found
Public Function SymbolsFindChartName(ByRef ChartName As String) As Long
Dim i As Long

For i = 1 To gSymbolsCount
If gSymbols(i).ChartName = ChartName Then
SymbolsFindChartName = i
Exit Function
End If
Next

End Function

' Returns the index in gSymbols() array, 0 if not found
Public Function SymbolsFindSymbol(ByRef Symbol As String) As Long
Dim i As Long

For i = 1 To gSymbolsCount
If gSymbols(i).Symbol = Symbol Then
SymbolsFindSymbol = i
Exit Function
End If
Next

End Function



From: Phil Hunt on
Given there are only two items in the structure, is this easier with
Dictionary ?


First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5
Prev: Datagrid problem
Next: Virtual memory increasing.