Identify Real Number

Identify Real Number

Post by bcoleman01 » Sun, 07 May 2006 03:52:20


I need to identify the real number in a string which is actually a file
name. Here are a few examples:

R55567E1-A-1.TXT I need 55567

R442A-B-2.TXT I need 442

R11258974-A-1.TXT I need 1125897

The constant in all the file names are that the real number I need
follows the R. Can I evaluate, beginning with the 2nd position, and
read until no real number is found?



Thanks,

Bill
 
 
 

Identify Real Number

Post by Richard Co » Sun, 07 May 2006 04:12:15


Bill

A bit of air code

Dim RealNumber as long
...
RealNumber = Clng(Mid$(filename,2))

Richard
Web pages: http://www.yqcomputer.com/ ,
http://www.yqcomputer.com/ ://www.homeindorset.co.uk
because I loves the domain name for email.
--
You know, I used to think I was stupid, and then I met philosophers. - 'Small
Gods' by Terry Pratchett

 
 
 

Identify Real Number

Post by Rick Roths » Sun, 07 May 2006 04:18:34

>I need to identify the real number in a string which is actually a file

You didn't say whether you wanted the number to be a String value or a
numerical value... I assumed numerical. Here is some code for you to look
at...

Dim X As Long
Dim FName As String
Dim Number As Long
FName = "R55567E1-A-1.TXT"
For X = 2 To InStr(FName, ".")
If Not Mid$(FName, X, 1) Like "#" Then
Number = CLng(Mid$(FName, 2, X - 2))
Exit For
End If
Next


Rick
 
 
 

Identify Real Number

Post by Ivar » Sun, 07 May 2006 04:22:06

As you describe things, the function below will return what you want.

Ivar

Private Function GetRealNum(FileName As String) As String
Dim I As Integer
Dim S As String
For I = 2 To Len(FileName)
If IsNumeric(Mid(FileName, I, 1)) Then
S = S & Mid(FileName, I, 1)
Else:
Exit For
End If
Next
GetRealNum = S
End Function
 
 
 

Identify Real Number

Post by Christophe » Sun, 07 May 2006 04:23:52

That would be it. I would do something like this aircode:

GetFileNumber([filename])

Function GetFileNumber(strFileName as String)

For x = 2 to Len(strFileName)
If isnumeric(mid(strFileName,x,1) Then
GetFileNumber = GetFileNumber & mid(strFileName,x,1)
Else
Exit For
End If
Next

End Function
 
 
 

Identify Real Number

Post by Rick Roths » Sun, 07 May 2006 04:27:54

>>I need to identify the real number in a string which is actually a file

CLng won't work, but I believe you were thinking of Val instead.
Unfortunately, that won't work either. Actually, it works, but the type of
String values makes it possible for Val to return an incorrect value. Using
the first filename as an example...

RealNumber = Val(Mid$(filename, 2)

will assign 555670 and not the correct value of 55567 to the RealNumber
variable. Why? Well, it is because the E1 after the actual number is
properly a power of ten designation and Val respects that. The number Val
sees is 55567E1.

Rick
 
 
 

Identify Real Number

Post by Richard Co » Sun, 07 May 2006 04:43:11

On Fri, 5 May 2006 15:27:54 -0400, "Rick Rothstein"


Rick

I did say it was air-code. I agree that your response to Bill should be the
way he should go.

Richard
Web pages: http://www.yqcomputer.com/ ,
http://www.yqcomputer.com/ ://www.homeindorset.co.uk
because I loves the domain name for email.
--
You know, I used to think I was stupid, and then I met philosophers. - 'Small
Gods' by Terry Pratchett
 
 
 

Identify Real Number

Post by erewho » Sun, 07 May 2006 16:22:39


I would write a totally generic routine that can be used in any App :-

Private Sub Command1_Click()

Me.Print ExtractFirstNumber("R55567E1-A-1.TXT")
Me.Print ExtractFirstNumber("R442A-B-2.TXT")
Me.Print ExtractFirstNumber("R11258974-A-1.TXT")

End Sub

Private Function ExtractFirstNumber&(ByVal S$)
Dim L9&, DoneFlag As Boolean
Dim C As String * 1

For L9 = 1 To Len(S$) + 1 ' force last
C = Mid$(S$, L9, 1)
If InStr("0123456789", C$) = 0 Then
Select Case DoneFlag
' --- it is a leading non-numeric
Case False: Mid$(S$, L9, 1) = " "
DoneFlag = True
' --- it is a trailing non-numeric
Case True:
If L9 <= Len(S$) Then
Mid$(S$, L9, 1) = "X"
End If
ExtractFirstNumber = Val(S$)
Exit For
End Select
End If
Next

End Function

In fact, I would make it even more generic :-

Private Sub Command2_Click()
Dim A$(3), L9&

A$(1) = "R55567E1-A-1.TXT"
A$(2) = "R442A-B-2.TXT"
A$(3) = "R11258974-A-1.TXT"

For L9 = 1 To 3
Me.Print A$(L9); " ";
Me.Print ExtractNthNumber(A$(L9), 1)
Next

Me.Print
For L9 = 1 To 3
Me.Print A$(L9); " ";
Me.Print ExtractNthNumber(A$(L9), 2)
Next

Me.Print
For L9 = 1 To 3
A$(L9) = A$(L9) + "123"
Me.Print A$(L9); " ";
Me.Print ExtractNthNumber(A$(L9), 3)
Next

End Sub

Private Function ExtractNthNumber&(S$, Nth&)
Dim L9&, Count&, Start&
Dim LastNumericFlag As Boolean
Dim C As String * 1

' --- Find start of Nth Number
For L9 = 1 To Len(S$)
C = Mid$(S$, L9, 1)
Select Case (InStr("0123456789", C$) > 0)
' --- Numeric
Case True:
If LastNumericFlag = False Then
Count = Count + 1
If Count = Nth Then
Start = L9
Exit For
End If
End If
LastNumericFlag = True
' --- Not Numeric
Case False:
LastNumericFlag = False
End Select
Next

' --- Failed
If Start = 0 Then
Exit Function
End If

Count = 1
For L9 = Start To Len(S$) + 1 ' force last
C = Mid$(S$, L9, 1)
If InStr("0123456789", C$) = 0 Then
ExtractNthNumber = Val(Mid$(S$, Start, Count))
Exit For
End If
Count = Count + 1
Next

End Function