By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,661 Members | 1,311 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,661 IT Pros & Developers. It's quick & easy.

comma seperated list comparison

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.