473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Must be more elegant solution than this?

Hi Everyone,

Is there a better way of doing the following?

I have 2 lists. List 1 is a list of MAC addresses and List 2 is a list
of regular expressions. A user is only allowed to view devices that
match their list of regular expressions.

Small example:

List1
-------
0011223344
0022334455
0011225544

List2
-------
001122.*
002233.*

Now the only way I can see of doing this is to have a foreach loop for
List 1 and then nest a foreach loop for list2.

Pseudo Code

foreach(item in list1)
{

foreach(item in list2)
{
if (list1.item is a match on list2 regular expression)
{
Write(This device is allowed!)
break;
}
}
}

Seems a very inefficient way of doing it but can;t think of any othe
way to match list1 to list2? BTW, List1 could get quite large and
List2 could be a fair size as well.

Any ideas,

Thanks
Jun 28 '06 #1
6 1341
"Mike Davies" <mike@(cutmeout)scrappy.freeserve.co.uk> wrote in message
news:h5********************************@4ax.com...
Now the only way I can see of doing this is to have a foreach loop for
List 1 and then nest a foreach loop for list2.
I can't think of an easy way to improve on this, either.
Seems a very inefficient way of doing it


Try it, measure it, and see if it's worth spending any time trying to
optimize.

"Premature optimization is the root of all evil." (Hoare via Knuth)
Jun 28 '06 #2
"Mark Wilden" <Ma********@newsgroups.nospam> wrote in message
news:ea**************@TK2MSFTNGP03.phx.gbl...
"Mike Davies" <mike@(cutmeout)scrappy.freeserve.co.uk> wrote in message
news:h5********************************@4ax.com...
Now the only way I can see of doing this is to have a foreach loop for
List 1 and then nest a foreach loop for list2.
I can't think of an easy way to improve on this, either.


Quickly, it would be far better to put the strings loop inside the regex
loop, because reusing a regex is much more efficient than creating a new
one.

If this turns out to be a performance bottleneck, some more ideas:
You might also get a win by extracting the leading literal characters from
the regex. Now you will only need to do a regex test on the addresses that
start with the right digits. Note that this may not help certain regexs at
all. Sorting your address list, and now getting the subset of potential
matches requires a binary search. This is oriented toward address lists
orders of magnitude larger than the regex list. Also flag addresses on the
first successful match to avoid testing them against each successive regex.
Seems a very inefficient way of doing it


Try it, measure it, and see if it's worth spending any time trying to
optimize.

"Premature optimization is the root of all evil." (Hoare via Knuth)

Jun 28 '06 #3
Another thing you should add is to remove items that have already been
matched from the items you continue trying to match :) think about the
following data ..

100000
..
..
123450
first item is 1*, there are then 70 other regexps to run ... the first
expression will in fact select every node.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
"Mike Davies" <mike@(cutmeout)scrappy.freeserve.co.uk> wrote in message
news:h5********************************@4ax.com...
Hi Everyone,

Is there a better way of doing the following?

I have 2 lists. List 1 is a list of MAC addresses and List 2 is a list
of regular expressions. A user is only allowed to view devices that
match their list of regular expressions.

Small example:

List1
-------
0011223344
0022334455
0011225544

List2
-------
001122.*
002233.*

Now the only way I can see of doing this is to have a foreach loop for
List 1 and then nest a foreach loop for list2.

Pseudo Code

foreach(item in list1)
{

foreach(item in list2)
{
if (list1.item is a match on list2 regular expression)
{
Write(This device is allowed!)
break;
}
}
}

Seems a very inefficient way of doing it but can;t think of any othe
way to match list1 to list2? BTW, List1 could get quite large and
List2 could be a fair size as well.

Any ideas,

Thanks

Jun 28 '06 #4

Mike Davies (cutmeout) wrote:
Hi Everyone,

Is there a better way of doing the following?

I have 2 lists. List 1 is a list of MAC addresses and List 2 is a list
of regular expressions. A user is only allowed to view devices that
match their list of regular expressions.

Small example:

List1
-------
0011223344
0022334455
0011225544

List2
-------
001122.*
002233.*

Now the only way I can see of doing this is to have a foreach loop for
List 1 and then nest a foreach loop for list2.

Pseudo Code

foreach(item in list1)
{

foreach(item in list2)
{
if (list1.item is a match on list2 regular expression)
{
Write(This device is allowed!)
break;
}
}
}

Seems a very inefficient way of doing it but can;t think of any othe
way to match list1 to list2? BTW, List1 could get quite large and
List2 could be a fair size as well.


As Greg Young suggests, your method can be improved by noting that at
the moment, you continue to check a device even when it has already
been matched!

Remembering that we can't modify a collection while enumerating it(*),
here is some pseudo code:

Create all the regexes up front
Create an address collection named ToCheck that contains all the
addresses
Create empty address collections named OkAddresses and StillToCheck

foreach regex
foreach address in ToCheck
if current regex matches address
add current address to OkAddresses
else
add current address to StillToCheck

set ToCheck to StillToCheck, and clear out StillToCheck

now OkAddresses is the ok addresses

(*) is why we can't just remove ok addresses out of ToCheck. Also, if
possible order your regexes so that the most inclusive will be run
first.

--
Larry Lard
Replies to group please

Jun 29 '06 #5
Hi Mike,

First of all, it looks from your example, that you don't necessarily need to
use Regular Expressions at all. Unless there is something you need to match
other than the beginning of the string, you certainly do not need to use
Regular Expressions, and they might, in fact, impede performance.

If all you need to do is match the beginning of the string, first, sort both
lists. Then start with the first item in the user's list, and read all
members of the second list using IndexOf(list1) to determine if that entry
starts with the string from the first list. At the point where it returns 0,
you have found the first match. When it returns -1, you have found the last
match for that entry. The end point is now the starting point of the second
loop though the second list, and you repeat this process until you reach the
end of the first list.

User's Sorted List (listA):

001122
002233
002245

Sorted Available Mac Addresses (listB):

0000000000 - no match
0011112345 - no match
0011234567 - no match
0011223344 - matches [0]
0011223456 - matches [0]
0011234567 - no match
0012345678 - no match
0022334455 - matches [1]
0022334566 - matches [1]
0022345678 - no match
0022346678 - no match
0022455667 - matches [2]
0022456789 - matches [2]

bool[] matches = new bool[listB.Length]

int i = 0, ct = 0;
while (i < listA.Length)
{
while(listB[ct].IndexOf(listA[i]) != 0); // skip non-matches
{
listB[ct] = false;
ct++;
}
while(listB[ct].IndexOf(listA[i]) == 0) // find all matches
{
listB[ct] = true;
ct++:
}
i++;
}

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Big thicks are made up of lots of little thins.

"Mike Davies" <mike@(cutmeout)scrappy.freeserve.co.uk> wrote in message
news:h5********************************@4ax.com...
Hi Everyone,

Is there a better way of doing the following?

I have 2 lists. List 1 is a list of MAC addresses and List 2 is a list
of regular expressions. A user is only allowed to view devices that
match their list of regular expressions.

Small example:

List1
-------
0011223344
0022334455
0011225544

List2
-------
001122.*
002233.*

Now the only way I can see of doing this is to have a foreach loop for
List 1 and then nest a foreach loop for list2.

Pseudo Code

foreach(item in list1)
{

foreach(item in list2)
{
if (list1.item is a match on list2 regular expression)
{
Write(This device is allowed!)
break;
}
}
}

Seems a very inefficient way of doing it but can;t think of any othe
way to match list1 to list2? BTW, List1 could get quite large and
List2 could be a fair size as well.

Any ideas,

Thanks

Jun 29 '06 #6
Thanks everyone. That was useful.

On Wed, 28 Jun 2006 19:17:44 GMT, Mike Davies
<mike@(cutmeout)scrappy.freeserve.co.uk> wrote:
Hi Everyone,

Is there a better way of doing the following?

I have 2 lists. List 1 is a list of MAC addresses and List 2 is a list
of regular expressions. A user is only allowed to view devices that
match their list of regular expressions.

Small example:

List1
-------
0011223344
0022334455
0011225544

List2
-------
001122.*
002233.*

Now the only way I can see of doing this is to have a foreach loop for
List 1 and then nest a foreach loop for list2.

Pseudo Code

foreach(item in list1)
{

foreach(item in list2)
{
if (list1.item is a match on list2 regular expression)
{
Write(This device is allowed!)
break;
}
}
}

Seems a very inefficient way of doing it but can;t think of any othe
way to match list1 to list2? BTW, List1 could get quite large and
List2 could be a fair size as well.

Any ideas,

Thanks

Jun 29 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Kamilche | last post by:
Is there a more elegant way to change the working directory of Python to the directory of the currently executing script, and add a folder called 'Shared' to the Python search path? This is what...
17
by: Fresh Air Rider | last post by:
Hello Could anyone please explain how I can pass more than one arguement/parameter value to a function using <asp:linkbutton> or is this a major shortfall of the language ? Consider the...
2
by: Rosa | last post by:
Hi, I'm looking for an elegant solution on how to find the youngest file within a given directory. At the moment I'm storing all files in an array and loop through it comparing the creation date...
8
by: Braky Wacky | last post by:
Hello, I have an ASP.NET webpage that uses an instance of System.Web.UI.HtmlControls.HtmlInputFile for uploading files to our server. I came across the documentation at MSDN for upping the...
13
by: Edward W. | last post by:
hello, I have this function below which is simple and easy to understand private function ListHeight (byval UserScreenHeight as int) as int if UserScreenHeight < 1024 return 30 else return 50...
4
by: Iain King | last post by:
When I loop over one list I use: for item in items: print item but often I want to loop through two lists at once, and I've been doing this like I would in any other language - creating an...
32
by: r.z. | last post by:
class vector3 { public: union { float data; struct { float x, y, z; };
35
by: Bjoern Hoehrmann | last post by:
Hi, For a free software project, I had to write a routine that, given a Unicode scalar value U+0000 - U+10FFFF, returns an integer that holds the UTF-8 encoded form of it, for example, U+00F6...
5
by: Helmut Jarausch | last post by:
Hi, I'm looking for an elegant solution to the following (quite common) problem: Given a string of substrings separated by white space, split this into tuple/list of elements. The problem...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.