Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Extract segments of an array

29 views
Skip to first unread message

Tatsujin

unread,
Feb 5, 2021, 5:43:26 PM2/5/21
to
My array of strings is already sorted from A to Z:

mySort(1) = "apples"
mySort(2) = "apricot"
mySort(3) = "banana"
mySort(4) = "beans"
mySort(5) = "bread"
mySort(6) = "donuts"
'... etc, etc..
mySort(38) = "zuchini"

Is there an easy function that can copy any alphabetical segment of the array into a new array?

For example, if I give the function input "B", it should create this new array:

myarray = Array("banana", "beans", "bread")

Thanks!

Peter T

unread,
Feb 6, 2021, 8:38:10 AM2/6/21
to
"Tatsujin" <tatsuj...@gmail.com> wrote in message
Dim i as Long, k as Long
Dim myarray () As String
' code
ReDim myarray (1 To 3) ' or 0 to 2
For i = 3 To 5
k = k + 1
myarray (k) = mySort(i)
Next

There are other efficient ways to copy entire or part arrays but complicated
with string arays. The worksheet Index function would do it but not
efficient and much faster to loop as above.

Peter T


Claus Busch

unread,
Feb 6, 2021, 10:16:35 AM2/6/21
to
Hi,

Am Fri, 5 Feb 2021 14:43:22 -0800 (PST) schrieb Tatsujin:

> My array of strings is already sorted from A to Z:
>
> mySort(1) = "apples"
> mySort(2) = "apricot"
> mySort(3) = "banana"
> mySort(4) = "beans"
> mySort(5) = "bread"
> mySort(6) = "donuts"
> '... etc, etc..
> mySort(38) = "zuchini"
>
> Is there an easy function that can copy any alphabetical segment of the array into a new array?

if you don't know where your searched character starts in the array you
can search the words beginnig with your expected character with Reg.Exp:

Set re = CreateObject("vbscript.regexp")
myString = Join(mySort, ", ")
'modify the first character as expected
ptrn = "b.[a-z]{1,}"

re.Pattern = ptrn
re.Global = True
re.ignoreCase = True
Set matches = re.Execute(myString)
For Each match In matches
ReDim Preserve myArray(n)
myArray(n) = match
n = n + 1
Next


Regards
Claus B.
--
Windows10
Microsoft 365 for business

Peter T

unread,
Feb 6, 2021, 10:48:12 AM2/6/21
to
"Peter T" <as...@email.com> wrote in message

Seeing Claus' reply I realise I misread your question.

Peter T


Tatsujin

unread,
Feb 6, 2021, 7:42:26 PM2/6/21
to
On Saturday, February 6, 2021 at 8:16:35 AM UTC-7, Claus Busch wrote:
>
> Set re = CreateObject("vbscript.regexp")
> myString = Join(mySort, ", ")
> 'modify the first character as expected
> ptrn = "b.[a-z]{1,}"
>
> re.Pattern = ptrn
> re.Global = True
> re.ignoreCase = True
> Set matches = re.Execute(myString)
> For Each match In matches
> ReDim Preserve myArray(n)
> myArray(n) = match
> n = n + 1
> Next
>

I changed the mysort() array to this:

mysort(1) = "apple"
mysort(2) = "apricot"
mysort(3) = "baby"
mysort(4) = "banana"
mysort(5) = "bar"
mysort(6) = "fit"

When I use the search pattern of "a", it sets myarray to this:
myarray = ["apple", "apricot", "aby", "anana"].

It appears that your regex is finding anything with an "a" in the string. A search by "a" should have returned just "apple" and "apricot".

Claus Busch

unread,
Feb 6, 2021, 8:30:12 PM2/6/21
to
Hi,

Am Sat, 6 Feb 2021 16:42:22 -0800 (PST) schrieb Tatsujin:

> I changed the mysort() array to this:
>
> mysort(1) = "apple"
> mysort(2) = "apricot"
> mysort(3) = "baby"
> mysort(4) = "banana"
> mysort(5) = "bar"
> mysort(6) = "fit"
>
> When I use the search pattern of "a", it sets myarray to this:
> myarray = ["apple", "apricot", "aby", "anana"].

sorry, my bad.

to find the "a" at the beginning of the word you have to insert \bin
front of the pattern:
ptrn = "\ba[a-z]{1,}"

Tatsujin

unread,
Feb 6, 2021, 8:41:25 PM2/6/21
to
Cool! That pattern works!

But how do you test when no matches are found? I think it's returning the last element of the array when I search for a letter not in the array, such as "x".

Tatsujin

unread,
Feb 6, 2021, 9:18:57 PM2/6/21
to
I mean, there's an error message if I search for a missing letter in the array.

How do you test for an empty array? I thought "IsEmpty()" would work?

Tatsujin

unread,
Feb 6, 2021, 10:33:28 PM2/6/21
to
On Saturday, February 6, 2021 at 6:30:12 PM UTC-7, Claus Busch wrote:
> Hi,
> Am Sat, 6 Feb 2021 16:42:22 -0800 (PST) schrieb Tatsujin:
>

The code works great! Ignore my previous post about testing for an empty array. I merely need to check the count of the variable n to determine the array size. Thanks Claus!

Tatsujin

unread,
Feb 7, 2021, 2:06:16 AM2/7/21
to
On Saturday, February 6, 2021 at 6:30:12 PM UTC-7, Claus Busch wrote:
> Hi,
> Am Sat, 6 Feb 2021 16:42:22 -0800 (PST) schrieb Tatsujin:
>
> > I changed the mysort() array to this:
> >
> > mysort(1) = "apple"
> > mysort(2) = "apricot"
> > mysort(3) = "baby"
> > mysort(4) = "banana"
> > mysort(5) = "bar"
> > mysort(6) = "fit"
> >
> > When I use the search pattern of "a", it sets myarray to this:
> > myarray = ["apple", "apricot", "aby", "anana"].
> sorry, my bad.
>
> to find the "a" at the beginning of the word you have to insert \bin
> front of the pattern:
> ptrn = "\ba[a-z]{1,}"
>

Hi Claus. Do you know how to modify that regex pattern so that it grabs the entire string after the first matched letter?

So, if mysort(1) = "Andy is in Room #29.", then myarray(0) should have "Andy is in Room #29."


Claus Busch

unread,
Feb 8, 2021, 11:46:10 AM2/8/21
to
Hi,

Am Sat, 6 Feb 2021 23:06:11 -0800 (PST) schrieb Tatsujin:

> Hi Claus. Do you know how to modify that regex pattern so that it grabs the entire string after the first matched letter?
>
> So, if mysort(1) = "Andy is in Room #29.", then myarray(0) should have "Andy is in Room #29."

RegExp is for searching words or substrings in a text but not for
searching sentences.

dpb

unread,
Feb 8, 2021, 12:04:21 PM2/8/21
to
On 2/8/2021 10:46 AM, Claus Busch wrote:
> Hi,
>
> Am Sat, 6 Feb 2021 23:06:11 -0800 (PST) schrieb Tatsujin:
>
>> Hi Claus. Do you know how to modify that regex pattern so that it grabs the entire string after the first matched letter?
>>
>> So, if mysort(1) = "Andy is in Room #29.", then myarray(0) should have "Andy is in Room #29."
>
> RegExp is for searching words or substrings in a text but not for
> searching sentences.
>

For limited definitions of what constitutes a sentence it can be done.
One stackoverflow submission of many.
<https://stackoverflow.com/questions/5553410/regular-expression-match-a-sentence>

--

0 new messages