coding puzzle

gelo

Member
Joined
Sep 24, 2007
Messages
13
Programming Experience
1-3
heyo new here,

ive been writing a program to edit a special file that is a text based mesh file. In the program i want to make a special replace feature. ive tried so many times but to no avail...

im useing visual basic express 2005

When i get my prog to search for the substring "material="#"" (# = any number including -1)in multiple strings,

i want the first material="#" its found to be changed to material="0",

and the second material="#" its found to be changed to material="1"

and the third material="#" its found to be changed to material="2" and so on and so forth

all this is done via a button on a separate form. the material="#" is in a richtextbox in the main form

if you can get my gist lol, is it possible to do that? if you thought of any code i would be so greatfull for it. with the help youve supplied this site will be referenced in the help box including the person with the code that works.

THANKS!
 
Last edited:
I would suggest you learn regular expressions that is used to search/replace string patterns. One example here that seems to work if I understood the requirements:
VB.NET:
'Imports System.Text.RegularExpressions

Private Sub processfile()
    Dim pattern As String = "material=""-?\d+"""
    Dim input As String = My.Computer.FileSystem.ReadAllText("in.txt")
    counter = -1
    Dim output As String = Regex.Replace(input, pattern, AddressOf ReplaceNumber)
    My.Computer.FileSystem.WriteAllText("out.txt", output, False)
End Sub

Private counter As Integer
Private Function ReplaceNumber(ByVal m As Match) As String
    counter += 1
    Return Regex.Replace(m.Value, "-?\d+", counter.ToString)
End Function
Short explanation: the "material" pattern is searched first, by using a MatchEvaluator delegate (ReplaceNumber method) the custom replace is done by only searching and replacing the number pattern (optional '-' sign and any digits) in each match with the counter value.
 
what type of Visual basic is this? since some declarations arnt true....

VB cant recognize "Replacenumber" so is it that dim'ed as an integer?

also

what is the m in "m.value"?

i plan on executing the code in a single click by the way... :D
 
Last edited:
ReplaceNumber is the function I posted. Did you import the namespace Imports System.Text.RegularExpressions ? You can call processfile method with a Button and click it ;) Btw, language is VB.Net of course, targeted at your stated version 2.0.
 
ok i understand how it works, its just that ive tried to fiddle with it so it works with the text strings in the richtextbox but it doesnt seam to like the m.value still or replacenumber. my bad or what LOL, i coded stuff like this before but i forgot how it was done... lol

i think i said before that iplan to make it work in a single click

VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If matinc.Checked() Then

            Dim counter As Integer
            Dim Replacenumber As Integer
            Dim pattern As String = "material=""-?\d+"""
            Dim input As String = mainf.RichTextBox1.Text
            counter = -1
            Dim output As String = Regex.Replace(input, pattern, AddressOf Replacenumber)
            counter += 1
            Return
            Regex.Replace(m.Value, "-?\d+", counter.ToString)

thats what i modified it to but i think i mucked it up a lil. BTW i do have the Imports System.Text.RegularExpressions at the beginning of the code page.
 
Last edited:
The last three lines you posted doesn't make sense, I think you copied the code wrong. ReplaceNumber is a method of it's own and it have to remain that.
 
how about you change the prosessing method to a function that takes the input string and returns the output string, that way you can get the processed text and do what you want with it, be it displaying it to UI or saving it to a file:
VB.NET:
    Private Function processtext(ByVal input As String) As String
        Dim pattern As String = "material=""-?\d+"""
        counter = -1
        Return Regex.Replace(input, pattern, AddressOf ReplaceNumber)
    End Function

for example use this code in button click event handler:
VB.NET:
richtextboxOutput.Text = processtext(richtextboxInput.Text)
 
ok ive set up the new code you sent me and its fine appart from it's saying that:
VB.NET:
Private Function processtext(ByVal input As String) As String

"Statement is not valid in a namespace"

EDIT:

oh wait i changed it to this:
VB.NET:
Public Class FIXMATdia
    Private Function processtext(ByVal input As String) As String
        Dim pattern As String = "material=""-?\d+"""
        counter = -1
        Return Regex.Replace(input, pattern, AddressOf ReplaceNumber)
    End Function

but now it still says ReplaceNumber and counter is not declared. I think im being F***ing dumb here... i cant see why the hell it aint accepting it...
 
well, you have the button click method, right? these other methods aren't any different really, they are methods too, so you can copy them into the same location. like this in your form class file:

VB.NET:
sub button_click
'...
end sub

sub method1
'...
end sub

function method2
'...
end function
you have to get this right now, I want that help box enlisting now! :)
 
well it like the methods but still doesn't like counter or replacenumber LOL

sorry dude, you are trying so damn hard and i thank you for that, other forums havent even given a single post to this prob i have... you will be credited on the prog im making...

EDIT:
ah counter is working, when you posted about replacenumber did you imply that was a general term you made up?
 
Last edited:
well at this moment i have:
VB.NET:
Imports System.Windows.Forms
Imports System.Text.RegularExpressions

Public Class FIXMATdia
    Private counter As Integer
   
 Private Function ReplaceNumber(ByVal m As Match) As String
        counter += 1
        Return Regex.Replace(m.Value, "-?\d+", counter.ToString)
    End Function
    
Private Function processtext(ByVal input As String) As String
        Dim pattern As String = "material=""-?\d+"""
        counter = -1
        Return Regex.Replace(input, pattern, AddressOf Replacenumber)
    End Function
    
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        mainf.RichTextBox1.Text = processtext(mainf.RichTextBox1.Text)

        'End If

    End Sub

End Class

which seams right but i tested it and it does nothing. i think ive got something in the wrong order its ignoring something.
 
when you posted about replacenumber did you imply that was a general term you made up?
? no, ReplaceNumber is one of the methods posted first reply.

last post: looks right to me if FIXMATdia is your form class. btw, what is "mainf"? usually you just address the control directly (like post 7) or use Me.RichTextBox1.Text. Me is a reference to current class instance.
 
nope it does nothin, hmmm, im sure im missing something obvious...

and if it helps,

it looks through content like this:

HTML:
<HEADER MAGIC="XMF" VERSION="919" />
<MESH NUMSUBMESH="2">
    <SUBMESH NUMVERTICES="399" NUMFACES="664" NUMLODSTEPS="0" NUMSPRINGS="0" NUMMORPHS="0" NUMTEXCOORDS="1" MATERIAL="-1">
        <VERTEX NUMINFLUENCES="1" ID="0">
            <POS>47.2458 -73.3203 1101.75</POS>
            <NORM>0.577543 -0.650535 0.493202</NORM>
            <COLOR>1 1 1</COLOR>
            <TEXCOORD>0.576221 0.13896</TEXCOORD>
            <INFLUENCE ID="22">1</INFLUENCE>
        </VERTEX>
<FACE VERTEXID="0 1 2" />
</SUBMESH>
<SUBMESH NUMVERTICES="399" NUMFACES="664" NUMLODSTEPS="0" NUMSPRINGS="0" NUMMORPHS="0" NUMTEXCOORDS="1" MATERIAL="8">
        <VERTEX NUMINFLUENCES="1" ID="0">
            <POS>47.2458 -73.3203 1101.75</POS>
            <NORM>0.577543 -0.650535 0.493202</NORM>
            <COLOR>1 1 1</COLOR>
            <TEXCOORD>0.576221 0.13896</TEXCOORD>
            <INFLUENCE ID="22">1</INFLUENCE>
        </VERTEX>
<FACE VERTEXID="0 1 2" />
</SUBMESH>
bear in mind that there is going to be more than one:

HTML:
<SUBMESH NUMVERTICES="399" NUMFACES="664" NUMLODSTEPS="0" NUMSPRINGS="0" NUMMORPHS="0" NUMTEXCOORDS="1" MATERIAL="#">

so in that excerpt i would expect it to change the -1 to 0

and the next material it would change 8 to 1 and so on.
 
Last edited by a moderator:
"MATERIAL" isn't the same as "material" unless you specify Regex to use case-insensitive search.
 
Back
Top