Jump to content
Sign in to follow this  
anixon

String to Array

Recommended Posts

anixon

This one has got me stumped. The simply question is how do you convert a string to an array where the delimiter is a number of characters not " " or "|" etc

This could be the string:

$TestMessage = "Now is the time for all good men to come to the aid of the party Now is the time for all good men to come to the end of the party"

In this case the split was a maximum of 33 characters provided character 33 is equal to a space otherwise incremented by -1 until the first space is found and then [2] is processed from that character position using the same rules until the end of the string.

[0] 2

[1] Now is the time for all good men

[2] to come to the aid of the party

[3] Now is the time for all good men

[4] to come to the aid of the party

Help is always appreciated Ant.. Posted Image

Share this post


Link to post
Share on other sites
jchd

You could use StringRegExp to split on a fixed number N of characters, even if that number may need to be increased until the next whitespace.

There will be a problem however if the tail of the string split that way contains less than N characters (no match in this case). If this can occur, the best choice will be to perform the split by character-by-character examination.

Edit: your examples mis-counts. Applying your 33+ chars rule, we get the following split:

Now is the time for all good men to

come to the aid of the party Now is

the time for all good men to come

to the end of the party

And than can be done with a regexp, contrary to what I thought at first (using a strict rule), even with a short final tail.

$res = StringRegExp($str, "(.{33,}?\s|.*)", 1) oops, wrong, all wrong!

Try instead:

#Include <Array.au3>

Local $str = "Now is the time for all good men and women to come to the aid of the political_party_which_has_a_long_name Now is the time for all good men to come to the end of the party"
Local $res = StringRegExp($str, "(.{33,}?(?:\s)|.+)", 3)
_ArrayDisplay($res)

Sorry for multiple edits!

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
anixon

You could use StringRegExp to split on a fixed number N of characters, even if that number may need to be increased until the next whitespace.

There will be a problem however if the tail of the string split that way contains less than N characters (no match in this case). If this can occur, the best choice will be to perform the split by character-by-character examination.

Edit: your examples mis-counts. Applying your 33+ chars rule, we get the following split:

Now is the time for all good men to

come to the aid of the party Now is

the time for all good men to come

to the end of the party

And than can be done with a regexp, contrary to what I thought at first (using a strict rule), even with a short final tail.

$res = StringRegExp($str, "(.{33,}?\s|.*)", 1) oops, wrong, all wrong!

Try instead:

#Include <Array.au3>

Local $str = "Now is the time for all good men and women to come to the aid of the political_party_which_has_a_long_name Now is the time for all good men to come to the end of the party"
Local $res = StringRegExp($str, "(.{33,}?(?:\s)|.+)", 3)
_ArrayDisplay($res)

Sorry for multiple edits!

Given your location what else can I say but Tres Bon and thank you very much... Ant..Posted Image

Share this post


Link to post
Share on other sites
jchd

Again I apologize for editing my own errors multiple times while you were reading. The last change was making the separator whitespace (can be space, tab, cr) not captured, so that your strings don't end with space.

If you need any adjustment, you know where to chime (when being another story!).


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
anixon

Again I apologize for editing my own errors multiple times while you were reading. The last change was making the separator whitespace (can be space, tab, cr) not captured, so that your strings don't end with space.

If you need any adjustment, you know where to chime (when being another story!).

I can but thank you again for your prompt response and thank you for the kind offer of further assistance if required greetings and salutations - Antipodes Ant..

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×