Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
mac_h8r1
Minister of Gerbil Affairs
Topic Author
Posts: 2974
Joined: Tue Sep 24, 2002 6:57 pm
Location: Somewhere in the Cloud
Contact:

splitting strings using asp

Wed Feb 12, 2003 3:25 am

ok, my goal is to split strings into 8 character sections using asp, for example: "what would you like to do today??" would be translated into "what wou", "ld you li", "ke to do" " today??"
if anyone knows how to code this one up, the help would be greatly apriciated. thanks!
 
BigMadDrongo
Gerbil Elite
Posts: 909
Joined: Mon Apr 29, 2002 12:57 pm
Location: London, UK
Contact:

Wed Feb 12, 2003 4:09 pm

I forget if there's a Middle command (or some such thing - to take n characters from position a in a string), in which case you could use that in a For loop. Otherwise you could write a recursive function using Left (I forget the syntax, but just take the left 8 characters of the string, dump that into an array, and fill the rest of the array with the left 8 characters of what's left, etc, terminating when you've cut up the whole string).
 
boobyne
Gerbil XP
Posts: 354
Joined: Wed Jun 12, 2002 3:58 pm
Location: Mission Viejo, California

Wed Feb 12, 2003 4:28 pm

You didn't mention what you want to do with your string segments.

Basically, there are three string manipulation functions in VB Script. Left, mid, and right.

Jscript is more difficult to do string manipulation with. I don't do much Javascript, but I suggest you learn it so that you don't have the same problems that I do.

I also suggest that you install the MSDN Library so that you can press F1 to get context sensitive help.

There are many ways to do this. Here is one VBScript variation.

'find out how many elements will be in storage array
iStringLength = 8
iElements = len(sString) / iStringLength
'declare array
dim arStringArray(iElements)

'load array
i = 0
while len(sString) > 0
sTempString = left(sString, iStringLength)
arStringArray(i) = sTempString
if len(sTempString) > 8 then sStringArray = mid(sStringArray, iStringLength)
i = i + 1
wend
 
mac_h8r1
Minister of Gerbil Affairs
Topic Author
Posts: 2974
Joined: Tue Sep 24, 2002 6:57 pm
Location: Somewhere in the Cloud
Contact:

That would be nice

Wed Feb 12, 2003 5:17 pm

well, that would be nice if "Microsoft VBScript compilation" didnt expect a "integer constant" when declaring the dim of an array :-/ (Error Type:
Microsoft VBScript compilation (0x800A0402)
Expected integer constant
/split.asp, line 7, column 18 )
(line 7: "dim arStringArray(iElements) " )
i know that iElements has a value (i tested it) but i guess it wants a hard number and not just a variable :-?
 
boobyne
Gerbil XP
Posts: 354
Joined: Wed Jun 12, 2002 3:58 pm
Location: Mission Viejo, California

Wed Feb 12, 2003 5:24 pm

I think that in this kind of script all the variables are variants of a certain type, i.e. string, date, etc. Sometimes you have to force a conversion to the type that you need. In this case it think

iElements = cint(len(sString) / iStringLength)

or

dim arStringArray(cint(iElements))

will work.
Last edited by boobyne on Wed Feb 12, 2003 5:28 pm, edited 1 time in total.
 
boobyne
Gerbil XP
Posts: 354
Joined: Wed Jun 12, 2002 3:58 pm
Location: Mission Viejo, California

Wed Feb 12, 2003 5:27 pm

if that doesn't do it, try

dim arStringArray(with nothing, a zero, or a 1)

iElements = cint(len(sString) / iStringLength)

redim arStringArray(iElements)

Are you sure that you need to involve arrays in order to complete your task? They're good to know, but can get complicated.
 
mac_h8r1
Minister of Gerbil Affairs
Topic Author
Posts: 2974
Joined: Tue Sep 24, 2002 6:57 pm
Location: Somewhere in the Cloud
Contact:

maybe?

Thu Feb 13, 2003 1:43 am

haha with all the help youve given me on the subject (thanks a bunch for!) i sure hope so, but here, i'll explain what im trying to do: Create a binary -> character translator, so it will take a string (1001010001011010 for example), break it into bits, translate the bits into characters and output the correcponding compiled line of text that it codes for. Thanks again for your help

by the way, none of the above, methods worked (the first 2 still end at the same place, and the last one ending, saying that "Subscript out of range: 'x'" ending on the line containing "arStringArray(x) = sTempString ") :-/ Maybe we could use the left and replace function? ive had problems with that, maybe you could figgure that out out better than i could.

BigMadDrongo, im still working on your method...(using replace)
 
boobyne
Gerbil XP
Posts: 354
Joined: Wed Jun 12, 2002 3:58 pm
Location: Mission Viejo, California

Thu Feb 13, 2003 9:34 am

"subscript out of range" means that you are tying to access an array element that isn't dimensioned, in this case arStringArray(8) when your array has been defined to only inlcude 8 parts: 0-7. Remember that, unless otherwise defined as having a base of 1, the first element in an array starts at zero.

So, most likely, you are either not defining your array correctly or not incrementing your array correctly (i = i + 1 inside the loop). Print out what

Chances are, your translation has probably already been done. It may already be a function.

If you are translating a binary string, do you start from the front (left) or the back (right)?

There is a problem with this line:

if len(sTempString) > 8 then sStringArray = mid(sStringArray, iStringLength)

first of all it should be

if len(sTempString) > 8 then sString = mid(sString, iStringLength)

in order to redefine the string itself, therefore the string gets shorter with every iteration of the loop. Second, it doesn't redefine sString on the last time through the loop, and it should, or the loop will never end. So, try

if len(sTempString) > 8 then
'redefine string
sString = mid(sString, iStringLength)
else
'we're done
sString = ""
end if

That's what you get for copying code from a forum.

You're doing it the hard way. I suggest searching for a function that already does what you want. For example, the split function will turn a delimited string into an array. You redefine your string to have delimiters and use it, but I'll bet there's a binary string translation function out there.
 
BigMadDrongo
Gerbil Elite
Posts: 909
Joined: Mon Apr 29, 2002 12:57 pm
Location: London, UK
Contact:

Thu Feb 13, 2003 9:37 am

BigMadDrongo, im still working on your method...(using replace)

I've no idea if mine is the best way - it probably isn't, recursion tends to cause headaches when used unnecessarily. I'm doing a CS degree at the moment and we were using the language ML for the entire last term - it's a functional language and so has no iteration, variables, looping etc, so I'm used to having to think of recursive solutions :)

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On