473,670 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3081
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******** *************@z 14g2000cwz.goog legroups.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,$s 2)
{
$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_di ff($tmp3,$small er_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******** *************@n ews-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******** *************@z 14g2000cwz.goog legroups.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,$s 2)
{
$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_di ff($tmp3,$small er_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(ar ray_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
5476
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 want is:
2
2718
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 (among other things). While I understand how to initialize a primitive data type with a comma separated list, e.g. int a = { 11, 23, 43 }; I know how to initialize the example object's "student1" and "student2" below, e.g.
11
2527
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 name | 1,2,3,4,5 2 | company 2 | 2,4
2
7729
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, 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
3
1648
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 contain a list of two IDs which are broken by a comma. For example: BENL LT, RAT LT At the moment, I've got the VBA bits working on the single ID stuff, but, should it come across a list like the above,
3
2985
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 REIPropertyType.Items) { if (lst.Selected == true) { REIPropertyTypeVal += lst.ToString() + ","; }
0
1858
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 know i can use cpytostmf/cpytoimpf to create the file, but how do i get the headings to be comma seperated while the rest of the file is tab delimited? Any help would be wonderful. Thank you
6
2578
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 (enam,sal,jdate,job). The below code gives me proper result. But I am facing problem with last column name .It comes with coma and then braket . I dont want that comma with last column name . Can u tell me how to achieve that . Please refer below code .. Thanks in advance
1
9713
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 the same to mysql stored procedure as a parameter. I want to use these values in where clause of select statement using IN. I can't use prepared statements because queries written in stored procedure are very complex.
0
8468
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8901
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8814
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8660
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5683
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4209
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4390
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2799
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2041
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.