472,328 Members | 1,021 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

comma seperated list comparison

is it possible to compare acomma separated list aginst another

eg comma list 1 => 1,2,3,4,5
comma list 2 => 3,5
can you check that 3 is in both, and 5 is in both, therfore they match???

the comparison is to check that if product a who supplies products 1,2,3,4,5
can be used instead of product b who supplies 3,5 as product a already
supplies them


Jul 17 '05 #1
7 2976
On 2005-04-11, Craig Keightley <do**@spam.me> wrote:
is it possible to compare acomma separated list aginst another

eg comma list 1 => 1,2,3,4,5
comma list 2 => 3,5
can you check that 3 is in both, and 5 is in both, therfore they match???

the comparison is to check that if product a who supplies products 1,2,3,4,5
can be used instead of product b who supplies 3,5 as product a already
supplies them


You can do this in PHP. First, don't use comma separated strings use
arrays instead. Then you can get the elements in both arrays with the
array_intersect() function.

BTW. in most cases it's bad karma to crosspost, it's worse to crosspost
to lots of groups and it's just horrible if you don't set a followup-to.

--
Cheers
- Jacob Atzen
Jul 17 '05 #2
Craig Keightley wrote:
is it possible to compare acomma separated list aginst another

eg comma list 1 => 1,2,3,4,5
comma list 2 => 3,5
can you check that 3 is in both, and 5 is in both, therfore they match???

the comparison is to check that if product a who supplies products 1,2,3,4,5
can be used instead of product b who supplies 3,5 as product a already
supplies them


Here's a MySQL solution that works for product values 0 through 99. It
uses a cartesian product to generate a list 0..99 (it's easy to add
another digit to make it 0..999). Then it uses a regular expression to
find the value in your comma-separated lists, using the MySQL regexp
token for word boundaries (to prevent "2" from matching "12" or "20").

The result set of this query is two rows, for the values that are common
to both lists: 3 and 5.

SELECT tens.d*10 + ones.d AS n
FROM (SELECT 0 AS d UNION ALL
SELECT 1 AS d UNION ALL
SELECT 2 AS d UNION ALL
SELECT 3 AS d UNION ALL
SELECT 4 AS d UNION ALL
SELECT 5 AS d UNION ALL
SELECT 6 AS d UNION ALL
SELECT 7 AS d UNION ALL
SELECT 8 AS d UNION ALL
SELECT 9 AS d) AS tens
INNER JOIN
(SELECT 0 AS d UNION ALL
SELECT 1 AS d UNION ALL
SELECT 2 AS d UNION ALL
SELECT 3 AS d UNION ALL
SELECT 4 AS d UNION ALL
SELECT 5 AS d UNION ALL
SELECT 6 AS d UNION ALL
SELECT 7 AS d UNION ALL
SELECT 8 AS d UNION ALL
SELECT 9 AS d) AS ones
WHERE "1,2,3,4,5" REGEXP CONCAT('[[:<:]]', (tens.d*10 + ones.d), '[[:>:]]')
AND "3,5" REGEXP CONCAT('[[:<:]]', (tens.d*10 + ones.d), '[[:>:]]')
ORDER BY n

(Unfortunately, we can't use the column alias 'n' in the WHERE clause.
This is a documented limitation of MySQL and the SQL language in general.)

You can replace the regexp comparison with an IN predicate if you can
interpolate your comma-separated lists as string into the SQL query:

....
WHERE (tens.d*10 + ones.d) IN ( $comma_list_1 )
AND (tens.d*10 + ones.d) IN ( $comma_list_2 )
ORDER BY n

Regards,
Bill K.
Jul 17 '05 #3
Ken that functions works a treat
I have modified it to do waht i need it to do by including it in a loop of
other lists and showing if they are a match or not

Many thanks

Craig
"Ken Robinson" <ke******@rbnsn.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...

Craig Keightley wrote:
is it possible to compare acomma separated list aginst another

eg comma list 1 => 1,2,3,4,5
comma list 2 => 3,5
can you check that 3 is in both, and 5 is in both, therfore they

match???

the comparison is to check that if product a who supplies products

1,2,3,4,5
can be used instead of product b who supplies 3,5 as product a

already
supplies them


Here's a pure PHP solution I just created:
<?
function comp_csv($s1,$s2)
{
$tmp1 = explode(',',$s1);
$tmp2 = explode(',',$s2);
$tmp3 = array_intersect($tmp1,$tmp2);
$smaller_array = (count($tmp1) < count($tmp2))?$tmp1:$tmp2;
$smaller_count = count($smaller_array);
$inboth = FALSE;
if (count($tmp3) == $smaller_count)
if (count(array_diff($tmp3,$smaller_array)) == 0) $inboth =
TRUE;
return($inboth);
}

$csv1 = '1,2,3,4,5';
$csv2 = '3,5';
$x = comp_csv($csv1,$csv2);
var_dump($x);
?>
The function:
1) creates temporary arrays from the CSV strings
2) gets the intersection between the two arrays, which should have no
more elements than the smaller array. It can have less.
3) if it has the same number of elements as the smaller array, it
checks whether the it's elements are the same as those in the smaller
array. If so, the returned value is set to TRUE

Ken
BTW, followups changed to just comp.lang.php

Jul 17 '05 #4
I can do the match perfectly but what i also need to do is create a third
list of comma separated values that are in both

eg:

List 1 => 1,2,3,4,5,6,7,8,11
List 2 => 1,3,4,5,6,7,10,23

Therefore

List 3 => 1,3,4,5,6,7

How do I populate this third list - I'm really stuck
"Craig Keightley" <do**@spam.me> wrote in message
news:42*********************@news-text.dial.pipex.com...
Ken that functions works a treat
I have modified it to do waht i need it to do by including it in a loop of
other lists and showing if they are a match or not

Many thanks

Craig
"Ken Robinson" <ke******@rbnsn.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...

Craig Keightley wrote:
is it possible to compare acomma separated list aginst another

eg comma list 1 => 1,2,3,4,5
comma list 2 => 3,5
can you check that 3 is in both, and 5 is in both, therfore they

match???

the comparison is to check that if product a who supplies products

1,2,3,4,5
can be used instead of product b who supplies 3,5 as product a

already
supplies them


Here's a pure PHP solution I just created:
<?
function comp_csv($s1,$s2)
{
$tmp1 = explode(',',$s1);
$tmp2 = explode(',',$s2);
$tmp3 = array_intersect($tmp1,$tmp2);
$smaller_array = (count($tmp1) < count($tmp2))?$tmp1:$tmp2;
$smaller_count = count($smaller_array);
$inboth = FALSE;
if (count($tmp3) == $smaller_count)
if (count(array_diff($tmp3,$smaller_array)) == 0) $inboth =
TRUE;
return($inboth);
}

$csv1 = '1,2,3,4,5';
$csv2 = '3,5';
$x = comp_csv($csv1,$csv2);
var_dump($x);
?>
The function:
1) creates temporary arrays from the CSV strings
2) gets the intersection between the two arrays, which should have no
more elements than the smaller array. It can have less.
3) if it has the same number of elements as the smaller array, it
checks whether the it's elements are the same as those in the smaller
array. If so, the returned value is set to TRUE

Ken
BTW, followups changed to just comp.lang.php


Jul 17 '05 #5
Craig Keightley wrote:
I can do the match perfectly but what i also need to do is create a third list of comma separated values that are in both

eg:

List 1 => 1,2,3,4,5,6,7,8,11
List 2 => 1,3,4,5,6,7,10,23

Therefore

List 3 => 1,3,4,5,6,7

How do I populate this third list - I'm really stuck


http://in.php.net/array

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #6
In article <42*********************@news-text.dial.pipex.com>,
Craig Keightley wrote:
I can do the match perfectly but what i also need to do is create a third
list of comma separated values that are in both

eg:

List 1 => 1,2,3,4,5,6,7,8,11
List 2 => 1,3,4,5,6,7,10,23

Therefore

List 3 => 1,3,4,5,6,7

How do I populate this third list - I'm really stuck


You can simply take each element of list 1, see if it is in list 2. If
it is, add it to the third list. When you're done with list 1, you're
done with list 3.

If you're trouble is with the language itself, use explode() to
transform your list into an array, then use either a nested for() or a
nested while(list() = each() ) to loop through it each member in list
1 and check if it is in list 2.

for( each x in list 1 )
for ( each y in list 2 )
is x = y ? if so, list_3[] = x;

Does that sound good?
Jul 17 '05 #7
Craig Keightley wrote:
I can do the match perfectly but what i also need to do is create a third
list of comma separated values that are in both

eg:

List 1 => 1,2,3,4,5,6,7,8,11
List 2 => 1,3,4,5,6,7,10,23

Therefore

List 3 => 1,3,4,5,6,7

How do I populate this third list - I'm really stuck


$arr_1 = array(1,2,3,4,5,6,7,8,11);
$arr_2 = array(1,3,4,5,6,7,10,23);

$arr_3 = array_intersect($arr_1, $arr_2);

print_r($arr_3);

Output:

Array
(
[0] => 1
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
)

If you want consecutive keys:

$arr_3 = array_values(array_intersect($arr_1, $arr_2));

HTH,
JP

--
Sorry, <de*****@cauce.org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Jul 17 '05 #8

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

Similar topics

3
by: Brad Joss | last post by:
Scenario: Table 1 (a id, b name) Table 2 (a FKid, d value) A standard join on a gives me something like: a1 b1 d1 a1 b1 d2 What I...
2
by: Pmb | last post by:
I'm trying to learn the syntax for initializing objects in a comma separated list. Below is an example program I wrote to learn how to do this...
11
by: Craig Keightley | last post by:
I have a mysql database with a list of companies who supply specific products tblSuppliers (simplified) sID | sName | goodsRefs 1 | comp...
2
by: Craig Keightley | last post by:
is it possible to compare acomma separated list aginst another eg comma list 1 => 1,2,3,4,5 comma list 2 => 3,5 can you check that 3 is in both,...
3
by: Gary Smith | last post by:
Hi, I've got a field that contains a list of rooms. In most cases, this contains a single ID. However, under some circumstances, the field may...
3
by: dfetrow410 | last post by:
I need make a comma seperated list, but whwn I build the list I get a comma at the end. How do I remove it? foreach (ListItem lst in...
0
by: Kristi | last post by:
I need to create a CL program that will take a PF, and create a tab delimited file that has comma seperated column headings as the first record. I...
6
by: orajit | last post by:
Hi, I wanted to list the all columns of a emp table . The output should be comma seperated and list of column should come in brakets like ...
1
by: udaypawar | last post by:
Hi All, I have one problem here with mysql stored procedures. I have a list of ids seperated by comma e.g., (" 'A', 'B', 'C' "). I am passing...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.