Parse-ing Strings

M

Martin Eyles

Hi,

I am getting a string passed back from a web form in the format
name1:variable1;name2:variable2...

I would like to split this up into several variable, such that the variable
names are name1, name2 etc. (I actually know what these names are) and the
values of the variables are variable1, variable2 etc.

I tried using split to split the string into an array at the semicolons, and
was then thinking I would split the value out with a second split command,
but this seems a bit of a complex way round the problem. Also I am having
some problems with the implementation.

My two questions are:
1. What is wrong with the code as it stands
2. What better way is there of acheiving the desired effect

My VB.NET code behind is something like:
'Declare Variables
Dim DataString As String
Dim DataArray(9) As String
'Get Data from http
DataString = Request("DataString")
'Parse Data
DataArray = TimeEntryData.Split(CChar(";")) 'This is the line that gives an
error
name1=TimeEntryDataArray(1)
Response.Write(name1)

And the Error I get is:
Object reference not set to an instance of an object.

Thanks,
Martin
 
P

Phillip N Rounds

In your code, what is TimeEntryData?
You seem to want to split 'DataString'
You should have DataArray = DataString.Split( CChar(";"))
 
M

Martin Eyles

Phillip N Rounds said:
In your code, what is TimeEntryData?
You seem to want to split 'DataString'
You should have DataArray = DataString.Split( CChar(";"))

Sorry, didn't finish renaming the variables for general-ness. The code
should read:
'Declare Variables
Dim DataString As String
Dim DataArray(9) As String
'Get Data from http
DataString = Request("DataString")
'Parse Data
DataArray = DataString.Split(CChar(";")) 'This is the line that gives an
error
name1=DataArray(1)
Response.Write(name1)
 
S

S. Justin Gengo [MCP]

Martin,

I think using the split function is a fine way to get your values. I'm not
aware of a better way. But perhaps someone else may chime in here on that.

That said here's a small example:

Dim TestData As String = "9;8;7;6;5;4;3;2;1;0"

Dim Data() As String = TestData.Split(CChar(";"))

Dim Value As String

For Each Test As String In Data

Value = Test

Next



Note that I'm not pre-defining the array.


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
M

Mythran

Martin Eyles said:
Hi,

I am getting a string passed back from a web form in the format
name1:variable1;name2:variable2...

I would like to split this up into several variable, such that the
variable names are name1, name2 etc. (I actually know what these names
are) and the values of the variables are variable1, variable2 etc.

I tried using split to split the string into an array at the semicolons,
and was then thinking I would split the value out with a second split
command, but this seems a bit of a complex way round the problem. Also I
am having some problems with the implementation.

My two questions are:
1. What is wrong with the code as it stands
2. What better way is there of acheiving the desired effect

My VB.NET code behind is something like:
'Declare Variables
Dim DataString As String
Dim DataArray(9) As String
'Get Data from http
DataString = Request("DataString")
'Parse Data
DataArray = TimeEntryData.Split(CChar(";")) 'This is the line that gives
an error
name1=TimeEntryDataArray(1)
Response.Write(name1)

And the Error I get is:
Object reference not set to an instance of an object.

Thanks,
Martin

Hrm...for something like this, I would use regular expressions...

Example:

Dim input As String = "name1=value1;name2=value2;name3=value3;"
Dim pattern As String = _
"(?<Name>[^=;]*)=(?<Value>[^=;]*);"
Dim matchCollection As MatchCollection = Regex.Matches(input, pattern)
For Each match As Match In matchCollection
Console.WriteLine( _
"Name: {0} - Value: {1}", _
match.Groups("Name").Value, _
match.Groups("Value").Value _
)
Next match

HTH,
Mythran
 
I

intrader

Hi,

I am getting a string passed back from a web form in the format
name1:variable1;name2:variable2...

I would like to split this up into several variable, such that the variable
names are name1, name2 etc. (I actually know what these names are) and the
values of the variables are variable1, variable2 etc.

I tried using split to split the string into an array at the semicolons, and
was then thinking I would split the value out with a second split command,
but this seems a bit of a complex way round the problem. Also I am having
some problems with the implementation.

My two questions are:
1. What is wrong with the code as it stands
2. What better way is there of acheiving the desired effect

My VB.NET code behind is something like:
'Declare Variables
Dim DataString As String
Dim DataArray(9) As String
'Get Data from http
DataString = Request("DataString")
'Parse Data
DataArray = TimeEntryData.Split(CChar(";")) 'This is the line that gives an
error
name1=TimeEntryDataArray(1)
Response.Write(name1)

And the Error I get is:
Object reference not set to an instance of an object.

Thanks,
Martin
Looks like you are transmitting a JavaScript object literal. This protocol
has been named JSON. You may find in open source forums code to split this
effectively. Note that is similar to splitting a name/value string (as
Form or QueryString have); so code in CPAN for doing this can also be
found there.
Now, since you already have some sort of Javascript object literal, why
don't you use JSscript to decode this? First enclose the hole thin in {},
then change your ':' to ":\'" and your ',' to, you get the idea. Then
instantiate the object in JScript and there you have it all nicely
separated.
 
M

Martin Eyles

S. Justin Gengo said:
Martin,


Dim Data() As String = TestData.Split(CChar(";"))

Note that I'm not pre-defining the array.

Even not pre-defining the array, I still get the same error. Any idea why?

Thanks,
Martin
 
M

Martin Eyles

intrader said:
On Thu, 12 Jan 2006 16:06:40 +0000, Martin Eyles wrote:

Looks like you are transmitting a JavaScript object literal. This protocol
has been named JSON. You may find in open source forums code to split this
effectively. Note that is similar to splitting a name/value string (as
Form or QueryString have); so code in CPAN for doing this can also be
found there.
Now, since you already have some sort of Javascript object literal, why
don't you use JSscript to decode this? First enclose the hole thin in {},
then change your ':' to ":\'" and your ',' to, you get the idea. Then
instantiate the object in JScript and there you have it all nicely
separated.

I think you have mis-understood what I am trying to do.

What is happening is that the user fills in a form in a pop-up window.
Javascript on the client side makes the string, and passes it from a pop-up
to a hidden input in the main window's form. The main window then closes the
pop-up and performs a post-back. Finally, the VB.net code on the server
decodes the string, and uses it as input to an SQL query, returning the
results with the posted page.

I think that the other answers are already covering what I am looking for on
this, but thanks for the info anyway,

Martin
 
M

Martin Eyles

Martin Eyles said:
Even not pre-defining the array, I still get the same error. Any idea why?

Thanks,
Martin

Found a potential source of the problem - the string is empty. But I am
confused, it should have all the data. I have tried both

Data = Request("Data")
and
Data = Request("Menu1_Data")

but both of these leave Data entirely empty. (note, Data is the name in the
menu.ascx file, but Menu1_Data is the name that appears on the page itself).

Thanks,
Martin
 
S

S. Justin Gengo [MCP]

Martin,

It wasn't until I read your reply to intrader that I realized I have an
example you may be able to use.

In the code library on my website:
http://www.aboutfortunate.com?page=codelibrary do a search for "close pop-up
and refresh" I have some example code for closing a pop-up window and
refreshing the main one and also calling code. It may help you get those
values back. I think I'm using a different technique than you, but it may
work for you.

Using the technique you'll find you can actually run your code within the
pop-up's page and then close it.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
M

Martin Eyles

S. Justin Gengo said:
Martin,

It wasn't until I read your reply to intrader that I realized I have an
example you may be able to use.

In the code library on my website:
http://www.aboutfortunate.com?page=codelibrary do a search for "close
pop-up and refresh" I have some example code for closing a pop-up window
and refreshing the main one and also calling code. It may help you get
those values back. I think I'm using a different technique than you, but
it may work for you.

Using the technique you'll find you can actually run your code within the
pop-up's page and then close it.

Thanks for the example. I don't think it does quite what I want (send data
back to the server), but that is OK, as I have solved the problem. I can in
fact use the fact that the form is server side, so rather than having to use
request, I can just use the variable name, and the language recognises it as
already declared. Thanks Anyway.

Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top