Results 1 to 14 of 14

Thread: Where to Start?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    7

    Where to Start?

    Hi,

    I am a DCS system engineer and wanted to write a quick tool to go through twenty (or a bit more) text files and search for a specific string, then take a other string and search for it in the same files and so on.

    Until VB6 I knew how to do this sort of stuf but after .NET, I got lost big time.

    So, what I would like to know is the best way foreward in learnig VB .NET FAST.

    You can stop reading from here but if your intersted in the tool I need then read on, and maybe give some hint and tips.

    The system I maintain has +- 40.000 tags
    Each tag has some parameters.

    example:

    TAG: pressure meter 1
    Lowscale=0
    Highscale=100
    AlarmLow=20
    AlarmHigh=80
    and so on
    and so on

    TAG: pressure meter 2
    Lowscale=0
    Highscale=1000
    AlarmLow=400
    AlarmHigh=750
    and so on
    and so on

    Now someone wants to know the Highscale and the AlarmHigh of 8000 tags, not all but 8000 out 40.000.

    He supply's me the tag, and I have to find the Highscale and the AlarmHigh. But I dont know in what file it is, all I know it is in one of the twenty files.

    I need more but once I get started I can figure it out myself.
    This is a very small sample of one of the 20+ files:


    NAME = GO_HCU_COM:130QR008 <<<<<<<<<<<<<< TAG
    TYPE = AIN
    DESCRP = HCU SULPHUR
    PERIOD = 3
    PHASE = 0
    LOOPID = 130Q008__ABC_ROM_AIN
    IOMOPT = 0
    IOM_ID =
    PNT_NO = 0
    SCI = 52
    HSCO1 = 100 <<<<<<<<<<<<< One line I need
    LSCO1 = 0
    DELTO1 = 1
    EO1 = PPM

    Well, hope to hear how to proceed, I dont mind some reading but I dont want to go to "school" because then it is cheaper and quicker to just go through these files myself or give a contractor the job

    Thanks in advance,
    Rob.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Where to Start?

    try this. i put the tags in a combobox to test it, but you can supply a string tag:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    4.         Dim basePath As String = "C:\Users\Paul\Desktop\New folder\"
    5.         Dim files() As String = {"fileName1.txt", "fileName2.txt", "fileName3.txt"}
    6.  
    7.         For Each file As String In files
    8.             Dim lines() As String = IO.File.ReadAllLines(basePath & file)
    9.             Dim startIndex As Integer = Array.FindIndex(lines, Function(s) s.StartsWith("NAME = " & ComboBox1.Text))
    10.             If startIndex = -1 Then Continue For
    11.  
    12.             For x As Integer = startIndex + 1 To lines.GetUpperBound(0)
    13.                 If lines(x).StartsWith("HSCO1 = ") Then
    14.                     MsgBox(lines(x).Substring(7))
    15.                     Return
    16.                 End If
    17.             Next
    18.  
    19.         Next
    20.  
    21.     End Sub
    22. End Class

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    7

    Re: Where to Start?

    Paul,

    Thanks for the program, this is more then I expected :-)
    But I dont know how to get it working. I changed the path and the file names and I did put a ComboBox1 on Form1.

    Then when I copy a tag in the ComboBox1 nothing happens :-(
    So I removed the ComboBox and used a Button and changed "StartsWith("NAME = " & ComboBox1.Text))" to StartsWith("NAME = " & "Real Tag name"))

    Then I can see that at least one file gets opend but I cant see a MsgBox.

    So, what am I doing wrong?

    Thanks in advance,
    Rob.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Where to Start?

    the StartsWith line must contain the exact (case sensitive) starting text of the line

  5. #5
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Where to Start?

    Quote Originally Posted by R0bbie View Post
    Until VB6 I knew how to do this sort of stuf but after .NET, I got lost big time.
    Why not do it in VB6 then?

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    7

    Re: Where to Start?

    Well, the main reason is that I dont have the software at home anymore and second, I do want to learn a little but not all there is about VB.NET.

    But after programming 6 hours now I am just at the point I can read some values, Thank's to Paul!

    The most time consuming is reading the helpfiles or search on the internet for solutions, I mean, this is not normal english anymore:
    startIndex As Integer = Array.FindIndex(lines, Function(s) s.StartsWith("NAME = " & ComboBox1.Text))

    Thanks anyway for the sugestion :-)

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,041

    Re: Where to Start?

    Yeah, nor is it needed. That single line replaces a fairly straightforward loop. You could iterate through the lines array in a much more familiar fashion like so:
    Code:
    Dim startIndex as Integer = -1
    For x = 0 to lines.Length
     If lines(x).StartsWith("NAME = " & ComboBox1.Text)) Then
      startIndex = x
      Exit For
     End If
    Next
    That would be the replacement for that single line. It also might perform faster. I haven't tested this particular example, but I have tested similar code, and found that the actual loop was a bit faster than the short alternative.

    Frankly, I find that people are being too quick to reach for LINQ and lambdas. It makes code look more arcane, and is shorter to write, but if it doesn't communicate well and doesn't perform as well, is it really better?
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    7

    Re: Where to Start?

    Hi Shaggy,

    This is a more readable format and it helped me a step further actualy :-)
    One thing I did not figured out is what a "lambda" exactly is because I get a warning that I should declare a variable (witch I did) but it still works

    So, the last thing I need is a array or a collection to write the extracted data to. It needs to be the same format for all tags and I did not find a solution yet.

    Little example?... oké, dont push me :-)
    tag1,HSCO1,LOSCI,HISCI,DESCR <maybe a couple more but you get the point
    tag2,HSCO1,LOSCI,HISCI,DESCR

    Not all tags will have a HISCI or a DESCR but it has to be a sort of excel sheet with rows and columns, where the appropiate collumn will have values.

    So, anayone got a sugestion, should I use a array or a collection <

    Thanks anyway for the kickstart!

  9. #9
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Where to Start?

    Quote Originally Posted by Shaggy Hiker View Post
    Frankly, I find that people are being too quick to reach for LINQ and lambdas. It makes code look more arcane, and is shorter to write, but if it doesn't communicate well and doesn't perform as well, is it really better?
    For some reason i can not explain why i am so drawn towards one line LINQ or lambdas examples. I spent around 1 and a half years learning vb6 back in 2000 and then moved over not Net a couple of years ago when i resumed programming. My point is i can definitely see the OP's confusion looking at pauls nice example so OP don't be put off by it but maybe don't be to quick in being drawn to LINQ.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,041

    Re: Where to Start?

    If you are talking about rows and columns (with those things you showed being columns, I presume), then you have two choices worth considering:

    1) A class with members for all of those items, then a List (of that class).

    2) A datatable.

    Frankly, I'd go with the latter. You could make the former work, and it actually fits into some philosophical considerations better, but you will do less work, and have an easier time, with the datatable. After all, it would fit into the mental model of the data better, since it really does have rows and columns. Furthermore, you could easily use it as the datasource for a DataGridView (you could also use the List of classes, too, but it might not be as obvious).

    To use the datatable, you would first need to create the table (very easy), then add the columns that you wanted (also easy, and there should be MANY examples around here or MSDN). Once you have that set up, you would just be adding a row for each record. That would consist of these three steps:

    1) Get a new row from the datatable (the .NewRow method)
    2) Fill in the fields for the new row (lots of examples around here, I wrote one yesterday or the day before).
    3) Add the row to the datatable.Rows collection (using the .Add method).
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    7

    Re: Where to Start?

    Pffffffff, MAN!

    Every solution i can think of adds two problems :-( Still having fun tho :-)

    Why does this happen:
    I have two arrays. One (DCSTAGS) with 2.500.000 lines, and to reduce the amount of lines I need to put all the the data belonging to one tag, on one line.

    The second array holds all the lines, starting with the tag and then all that belongs to that tag. (TAG2ROW)

    Tag2Row(Tagcounter) = Tag2Row(Tagcounter) & DCSTAGS(n) & ","

    This line puts all data from one tag (DCSTAGS(n)) in to one line in TAG2ROW

    This actualy works. BUT for some reason it puts a counter in front of the the tag.

    So this:
    NAME=70XV1201
    TYPE=GDEV

    Becomes this:
    0NAME=70XV1201,TYPE=GDEV

    Now, where did this zerro came from? I did not asked him to sit over there, nor did I wanted him to sit there, nor do I need him there!

    And to make things even worse, it does this also for 1,2,3,4 up to +- 45000

    Who knows how to get rid of this counter?
    Many thanks in advance,
    Rob.

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,041

    Re: Where to Start?

    We'd need to see some code to answer that, but the best answer is probably to go about this differently. I am beginning to think that I misunderstood what you wanted in post #8. Concatenating strings like that is terrible. If you are doing it only a few times, then that won't matter much, but a couple million times...that's gonna hurt! The problem is that working with strings is always slow. After all, when you concatenate two strings, the compiler doesn't take the first string and stick the second string onto the end, even though it looks like that is what it is doing. What it is actually going to do is create a new string the size of the two strings, copy the first one into the beginning, then copy the second into the end, then throw out the first two strings. Every time you use & or + to join two strings that are contained in variables (but not if they are literal strings), that is what you are doing. Therefore, you can count up every & in your lines, and that is the number of strings that might be created and destroyed (might not, too, because the compiler does have the ability to simplify things at times).

    The datatable would work FAR better than sticking a bunch of strings together, and might even get rid of the problem you are having. However, I guess it comes down to what the ultimate goal is. If you are doing all this concatenation so that you can save the data to a file, does it HAVE to be a CSV file (which is what you appear to be creating), and will you ever read the data back out of the file? If it doesn't HAVE to be CSV, then the datatable would allow you to create XML easily, but those files will be larger, because XML sticks lots of extra characters in along with the data.
    My usual boring signature: Nothing

  13. #13

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    7

    Re: Where to Start?

    Shaggy,

    What I need is one file, with a line for each tag and for each tag some parameters.

    One line has the tag 3 times, but in a slightly different format, this is because the list will be split for a couple of pc's that all work with the same tag but all need their own format to handle the tag.

    So the end result of one tag would be for example: DEGASSER:15PT1000.PNT

    Note that the tag varies a little each time, with a prefix, a dot becomes a line and so on.

    UNIT1_DEGASSER:15PT1000.PNT,DEGASSER:15PT1000_PNT,FACTORY1-DEGASSER-15PT1000.PNT,PC1/DEGASSER:15PT1000.PNT,HSCO1,SPAN,ALARMHIGH and so on.

    I used colors to indicate where the lines need to be split using excel, or I might decide to write the output to 3 files, but for now one file would be great.

    The end result will be that the tag can be monitored from around the world.
    But to go from internet to the DCS it needs to travel trough a couple of servers, all using their own format.

    Sorry if I made things complicated
    Kind regards,
    Rob.

    EDIT> And I was not awake when I wrote this replay
    The answer is yes, the file needs to be a CSV so I can import the file on those 3 computers I wrote about.
    Last edited by R0bbie; Mar 3rd, 2012 at 09:25 AM.

  14. #14

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    7

    Re: Where to Start?

    The code that puts a counter in the array is as follows,

    DIM Tag2Row as new arraylist

    For t = 1 To 10
    Tag2Row.Add(t)
    Next

    Now Tag2Row(0) contains a 0
    Tag2Row(1) contains a 1 and so on.

    So I must be doing something wrong but I know what.

    Regards,
    Rob.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width