# Visual basic: how to derive coordinates from an array value

### Dotaz

• Hi,

I've got an array that contains the following:

a;b;c;d

but it can also be that this array contains this:

a;b;c;d;(0,0,1000,1000)#(1000,1000,2000,0)

So first I do a split on ";"

If Ubound(array)= 4 then I now the second case with coordinates is valid... in this case 2 sets of 4 coordinates

How do I derive them from array(4) as xstart0,ystart0,xend0,yend0 and xstart1,ystart1,xend1,yend1 ???

Do I need to do another split on "#" first and then on the "," ?

Regards, Lars

30. dubna 2012 13:24

### Odpovědi

• Try this regular expression:

`(?<a>\d+);\s*(?<b>\w*\,\s\w*);\s*(?<c>\d{2}\.\d{2});\s*(?<d>\d*)(;((\(\d+,\d+,\d+,\d+\)(#)*)*))*`

It matched all of the entries you provided:

30. dubna 2012 20:21

### Všechny reakce

• How do I derive them from array(4) as xstart0,ystart0,xend0,yend0 and xstart1,ystart1,xend1,yend1 ???

Do I need to do another split on "#" first and then on the "," ?

Regards, Lars

yes, or you can also use a RegEx

Hannes

In a perfect world,
users would never enter data in the wrong form,
files they choose to open would always exist
and code would never have bugs.

C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

30. dubna 2012 13:38
• This sample asks for a regular expression. If all the data is like this, composing a RegEx wouldn't be of much difficulty. Can you post a real data sample?
30. dubna 2012 13:39
• If you prefer string split then here is one way to accomplish this

```        Dim stringValue As String = "a;b;c;d;(0,0,1000,1000)#(1000,1000,2000,0)"
Dim separator As Char() = {";"}
Dim stringArray As String() = stringValue.Split(separator, StringSplitOptions.RemoveEmptyEntries)

If stringArray.Count = 5 Then
Dim stringCoordinates As String = stringArray(4)
stringCoordinates = stringCoordinates.Replace("(", "").Replace(")", "")
separator = {"#"}
Dim coordinateArray As String() = stringCoordinates.Split(separator, StringSplitOptions.RemoveEmptyEntries)
separator = {","}
Dim coordinates As String() = coordinateArray(0).Split(separator, StringSplitOptions.RemoveEmptyEntries)
Dim xstart0 = coordinates(0)
Dim ystart0 = coordinates(1)
Dim xend0 = coordinates(2)
Dim yend0 = coordinates(3)
Dim xstart1 = coordinates(0 + 3)
Dim ystart1 = coordinates(1 + 3)
Dim xend1 = coordinates(2 + 3)
Dim yend1 = coordinates(3 + 3)
ElseIf stringArray.Count = 4 Then

End If
```

 A.m.a.L Hashim My Blog - Dot Net Goodies

30. dubna 2012 13:47
• This sample asks for a regular expression. If all the data is like this, composing a RegEx wouldn't be of much difficulty. Can you post a real data sample?

2677729;  Birch, White;           19.45;  2
1234990;  Birch, Natural;         17.50;  1
2662666;  Birch, Red;             15.65;  3

the "a"-value in a;b;c;d is an item id like e.g. 2677729. These coordinates are cut values of wood selected... So the user can choose to add cuts or not. So if he select 3 types of wood all of them can have cuts added... in the example above no cuts have been added by the user yet. But he is allowed to add up to 10 cuts so it could look like this:

2677729;  Birch, White;           19.45;  2
1234990;  Birch, Natural;         17.50;  1;(0,0,1000,1000)#(1000,1000,2000,0)
2662666;  Birch, Red;             15.65;  3;(0,0,800,0)#(100,100,200,400)#(400,0,2000,1750)

(the value of a coordinate can be between 0 and 2000. The sets of coordinates are independent from eachother so they don't need to make sense)

But what I first need to do is to find the correct ID in this file, and then add cuts to it.. But I haven't managed to find the correct variable from the other form to compare the ID in the text file with. Working on that one first...

30. dubna 2012 15:07
• Here's a question: do you need to get the individual coordinates in a set at any stage? If you do and use split that would add another stage: split on ';' then on '#' then on ','.

Regards David R
---------------------------------------------------------------
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute.

30. dubna 2012 15:47
• Try this regular expression:

`(?<a>\d+);\s*(?<b>\w*\,\s\w*);\s*(?<c>\d{2}\.\d{2});\s*(?<d>\d*)(;((\(\d+,\d+,\d+,\d+\)(#)*)*))*`

It matched all of the entries you provided:

30. dubna 2012 20:21