1

I have a question that could be easy or not.

I will explain further: I have a string like this:

str = "TR~Maintenance~fas plus-maintenicon|GR~Supplies~fas minus-suppicon|JK~Affidavit~fas minus-affiicon"

Now, my objective is to put those in an array separately like:

TR
Maintenance
fas plus-maintenicon
GR
Supplies
fas minus-suppicon
JK
Affidavit
fas minus-affiicon

and then reshape and redim the array to keep only, let's say, the strings starting with fas....

What is the easiest method? Spliting? regEx? inStr?

I have the following code:

Function funcSeparaLista(objLst)
    arrVLst = split(objLst, "~")
    
    for each x in arrVLst
        VLst = VLst&x&"<br/>"
    next
        
    funcSeparaLista = split(VLst, "|")
end function

t = funcSeparaLista(str)

But retrieving the data using:

response.write(t(0))
response.write(t(1))
response.write(t(2))
response.write(t(3))

I get undesired results:

TR
Maintenance
fas plus-mainteniconGR
Supplies
fas minus-suppiconJK
Affidavit
fas minus-affiicon

One thing most important: I want my FUNCTION only to deal with putting the detached string in arrays and dealing with it after in another portion of code.

After the array hold everything like I want it, I want only the array to keep this data:

fas plus-maintenicon
fas minus-suppicon
fas minus-affiicon

What I am doing wrong? Remember please. This is vbscript, not VB.Net. Thanks in advance I think this could be very easy but I am not quite trying to accomplish it after some hours redoing and checking code.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Joao
  • 85
  • 1
  • 9
  • 1
    It's a standard serialised string where `|` denotes a record and `~` denotes a field, so first use `Split()` to build an array of records by splitting on `|`, then in a loop `Split()` on `~` to get each field in the current record. – user692942 Mar 01 '22 at 13:28
  • 1
    If you haven't already considered it, do take a look at using a [scripting dictionary](https://www.vbsedit.com/html/b4a7ddb3-2474-49ef-8540-8d67a747c8db.asp) or [array list](https://www.robvanderwoude.com/vbstech_data_arraylist.php). See discussions [here](https://stackoverflow.com/q/13585660/15764378) and [here](https://stackoverflow.com/q/14479571/15764378) – LesFerch Mar 01 '22 at 15:39
  • Thanks LesFerch. It would be great but I cannot use scripting dictionary. The whole code in this app isn't only mine and was done in that manner to suit other structured code/directives etc... – Joao Mar 01 '22 at 16:18
  • @LesFerch personally, using COM objects when built-in objects like an array will suffice is overkill in my opinion. – user692942 Mar 02 '22 at 09:15

1 Answers1

0

This is a serialised string where the record delimiter is | and the field delimiter is ~. You can easily build a 2-Dimensional Array from this using a double loop.

Dim str: str = "TR~Maintenance~fas plus-maintenicon|GR~Supplies~fas minus-suppicon|JK~Affidavit~fas minus-affiicon"

Dim data: data = ConvertToArray(str, "|", "~")
WScript.Echo data(1, 2)

Function ConvertToArray(value, record_delim, field_delim)
    Dim data()
    If Len(value) < 1 Then ConvertToArray = Empty
    Dim records: records = Split(value, record_delim)
    If Not IsArray(records) Then ConvertToArray = Empty
    Dim rows: rows = UBound(records)
    Dim row, col
    For row = 0 To rows
        Dim fields: fields = Split(records(row), field_delim)
        If Not IsArray(fields) Then ConvertToArray = Empty
        Dim cols: cols = UBound(fields)
        ReDim Preserve data(cols, row)
        For col = 0 To cols
            data(col, row) = fields(col)
        Next
    Next
    ConvertToArray = data
End Function

Output:

Affidavit

Using this function you can get the output you require by looping through and returning just the 3rd column from each row.

Dim data: data = ConvertToArray(str, "|", "~")
If IsArray(data) Then
    Dim row
    Dim rows: rows = UBound(data, 2)
    For row = 0 To rows
        WScript.Echo data(2, row)
    Next
End If

Output:

fas plus-maintenicon
fas minus-suppicon
fas minus-affiicon
user692942
  • 16,398
  • 7
  • 76
  • 175
  • Thank you very much for your insight. I am gonna try it later and perhaps change the code to suit my needs. You say the output is only Affidavit? My goal is to retrieve: fas plus-maintenicon fas minus-suppicon fas minus-affiicon – Joao Mar 01 '22 at 16:23
  • 1
    @Joao The `ConvertToArray()` function takes the string and converts it into a 2-Dimensional array you can then loop through and pull out any combination of data you wish. The `data(1, 2)` is an example of pulling the second column in the 3rd row which is "Affidavit". – user692942 Mar 01 '22 at 17:54