Solved

sscanf function in VB?

Posted on 1998-05-18
8
1,788 Views
Last Modified: 2011-03-21
Is there a function in VB that is simmilar to the C function sscanf?
0
Comment
Question by:hubermar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 50 total points
ID: 1452832
There is no directly corresponding function but you can manipulate a string and convert text to numbers. If your string was of the form "FRED 1234 XYZ". you could write a sub such as

sub(text1 as string,numeric1 as double,text2 as string,target as string)
dim i as integer
dim length as integer
dim text3 as string

i = 1
length = len(target)

do until i > length or target(i) = " "
    text1 = text1 + target(i)
loop

i = i + 1

do until i > length or target(i) = " "
    text3 = text3 + target(i)
loop
numeric1 = val(text3)

i = i+1
     
do until i > length or target(i) = " "
    text2 = text2 + target(i)
loop
 
end sub
     
You'd have to tailor individual functions for individual circumstances - You could probably use variants for the variables and then select appropriate translation by 'chopping up'  a format string in the form "%d %s %d" etc

0
 
LVL 5

Expert Comment

by:bin_huwairib
ID: 1452833
No huberma, there is no function in VB is similar to sscanf, and I didn't find any API function equivalent to it, but you can use the normal string manipulation functions instead (like InStr, Mid, Left...).

Bin Huwairib
0
 
LVL 18

Expert Comment

by:deighton
ID: 1452834
CORRECTION TO STRING MANIPULATION

    sub(text1 as string,numeric1 as double,text2 as string,target as string)
    dim i as integer
    dim length as integer
    dim text3 as string

    i = 1
    length = len(target)

    do until i > length or mid(target,i,1) = " "
        text1 = text1 + mid(target,i,1)
    loop

    i = i + 1

    do until i > length or mid(target,i,1) = " "
        text3 = text3 + mid(target,i,1)
    loop
    numeric1 = val(text3)

    i = i+1
         
    do until i > length or mid(target,i,1) = " "
        text2 = text2 + mid(target,i,1)
    loop
     
    end sub
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:hubermar
ID: 1452835
deighton,

That's the method that I'm currently using.  It's a real pain.  I appreciate your suggestion.

Thanks,
Mark
0
 
LVL 18

Expert Comment

by:deighton
ID: 1452836
What are you trying to do exactly?  I can think of ways of improving my function.
0
 
LVL 18

Expert Comment

by:deighton
ID: 1452837
'Here's a more complcated example - hope it helps


Sub sscanf(ByVal sBuff As String, ByVal sFormat As String, Optional a, Optional b, Optional c, Optional d)

'Attempt to bring Sscanf function into VB
'uses optional variant parameters
'MAX four parameters in scanf string - but this COULD BE INCREASED

'EXAMPLE sscanf("1234 1.2 6","%i %d %i",a,b,c)
' puts 1234 in a (a is integer)
'        1.2 in b (d is a double)
'       6   in c (c is an integer)

Dim sTemp As String
Dim sID As String
Dim i As Integer
Dim i2 As Integer

'Extract paramater a

Call strip(sBuff, i, sTemp)
Call strip(sFormat, i2, sID)
Call value(sTemp, sID, a)

If IsMissing(b) Then Exit Sub

sBuff = Right(sBuff, Len(sBuff) - i - 1)
sFormat = Right(sFormat, Len(sFormat) - i2 - 1)

'extract parameter b

Call strip(sBuff, i, sTemp)
Call strip(sFormat, i2, sID)
Call value(sTemp, sID, b)

If IsMissing(c) Then Exit Sub

'You would repeat the sections to add new parameters e,f,g,... and put them in the parameter list


sBuff = Right(sBuff, Len(sBuff) - i - 1)
sFormat = Right(sFormat, Len(sFormat) - i2 - 1)

'extract parameter c

Call strip(sBuff, i, sTemp)
Call strip(sFormat, i2, sID)
Call value(sTemp, sID, c)

If IsMissing(d) Then Exit Sub

sBuff = Right(sBuff, Len(sBuff) - i - 1)
sID = Right(sFormat, Len(sFormat) - i2 - 1)

Call strip(sBuff, i, sTemp)
Call strip(sFormat, i2, sID)
Call value(sTemp, sID, d)

End Sub

Sub strip(sBuff As String, iReturn As Integer, sStrip As String)
    'Strip the next value or control code out of the string

    Dim i As Integer

    i = 1
    Do Until i > Len(sBuff) Or Mid(sBuff, i, 1) = " "
        i = i + 1
    Loop
   
    sStrip = Left(sBuff, i - 1)
    iReturn = i - 1

End Sub


Sub value(sExtract As String, sFormat As String, vResult)
   
    If sFormat = "%i" Then
        vResult = Int(Val(sExtract))
    ElseIf sFormat = "%s" Then
        vResult = sExtract
    ElseIf sFormat = "%d" Then
        vResult = Val(sExtract)
    End If
 
End Sub


'EXAMPLE

Private Sub Command1_Click()

    'example function call

    Dim i As Integer
    Dim s As String
    Dim d As Single
    Dim d2 As Double
    Call sscanf("1234 FRED 1592654 1", "%i %s %d %d", i, s, d, d2)
    MsgBox i
    MsgBox s
    MsgBox d
    MsgBox d2
End Sub

0
 

Author Comment

by:hubermar
ID: 1452838
deighton,

Very impressive.  I thank you for your help.  I will include this in my code.  I wasn't expecting so much help.  You went the extra mile to answer my question.  

Is it permissible to notify you when I ask questions in the future?  

Thanks again,
Mark
0
 
LVL 18

Expert Comment

by:deighton
ID: 1452839
E-Mail

DEIGHA@GUSCO.COM
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question