Find array with an array

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Find array with an array

jc-15
Is there a simple way to find out if a value within an array is also  
within another array?

Like this:
Array 1 = 1, 4, 5, 9, 11
Array 2 = 3, 6, 8, 10

No hit

Array 1 = 1, 4, 5, 9, 11
Array 2 = 3, 4, 8, 10

Hit

Array 1 = 1, 4, 5, 9, 11
Array 2 = 3, 4, 9, 10

Hit

HDB
JC

--
------------------------------
Lasso Support: http://support.omnipilot.com/
Search the list archives: http://www.listsearch.com/lassotalk.lasso
Manage your list subscription:  
http://www.listsearch.com/lassotalk.lasso?manage
Reply | Threaded
Open this post in threaded view
|

Re: Find array with an array

jasonhuck
[//lasso
        // initialize test arrays and set 'hit' to false
        var(
                'array1' = array(1,4,5,9,11),
                'array2' = array(3,6,8,10),
                'hit' = false
        );


        // (LP8 and higher)
        // check the size of the intersection of both arrays
        $array1->intersection($array2)->size ? $hit = true;
       
        $hit;


        // reset for a different test
        '<br>\n' * 2;
        $hit = false;


        // (LP7 compatible)
        // iterate through one array and look for matches in the other
        iterate: $array2, local: 'this';
                if: $array1 >> #this;
                        $hit = true;
                        loop_abort;
                /if;
        /iterate;

    $hit;
]



On 5/30/05, JC <[hidden email]> wrote:

> Is there a simple way to find out if a value within an array is also
> within another array?
>
> Like this:
> Array 1 = 1, 4, 5, 9, 11
> Array 2 = 3, 6, 8, 10
>
> No hit
>
> Array 1 = 1, 4, 5, 9, 11
> Array 2 = 3, 4, 8, 10
>
> Hit
>
> Array 1 = 1, 4, 5, 9, 11
> Array 2 = 3, 4, 9, 10
>
> Hit

--
------------------------------
Lasso Support: http://support.omnipilot.com/
Search the list archives: http://www.listsearch.com/lassotalk.lasso
Manage your list subscription:  
http://www.listsearch.com/lassotalk.lasso?manage
Reply | Threaded
Open this post in threaded view
|

Re: Find array with an array

jc-15
In reply to this post by jc-15
Intersection, what a beautiful piece of code...!
Thanks, Jason, I was hoping for something more elegant than using  
iterate. And you showed me!

Any idea of some kind of time penalties involved? Should the bigger  
array be first or second?

HDB
JC

30 maj 2005 kl. 15.52 skrev Jason Huck:

>     $array1->intersection($array2)->size ? $hit = true;
>


--
------------------------------
Lasso Support: http://support.omnipilot.com/
Search the list archives: http://www.listsearch.com/lassotalk.lasso
Manage your list subscription:  
http://www.listsearch.com/lassotalk.lasso?manage
Reply | Threaded
Open this post in threaded view
|

Re: Find array with an array

jc-15
In reply to this post by jc-15
Aha, that's not so cool. Next question therefor. How do I sort arrays?
One of the arrays I have in mind I control myself so I can make sure  
it's sorted but the other one is created automagically.

HDB
JC

30 maj 2005 kl. 16.17 skrev Mark Palmer:

> Hi,
>
> If the arrays weren't in order then they would need to be sorted for
> intersection to work correctly.
>
> on 30/5/05 14:52, Jason Huck at [hidden email] wrote:
>
>
>> [//lasso
>> // initialize test arrays and set 'hit' to false
>> var(
>> 'array1' = array(1,4,5,9,11),
>> 'array2' = array(3,6,8,10),
>> 'hit' = false
>> );
>>
>>
>> // (LP8 and higher)
>> // check the size of the intersection of both arrays
>> $array1->intersection($array2)->size ? $hit = true;
>>
>> $hit;
>>
>>
>> // reset for a different test
>> '<br>\n' * 2;
>> $hit = false;
>>
>>
>> // (LP7 compatible)
>> // iterate through one array and look for matches in the other
>> iterate: $array2, local: 'this';
>> if: $array1 >> #this;
>> $hit = true;
>> loop_abort;
>> /if;
>> /iterate;
>>
>>     $hit;
>> ]
>>
>>
>>
>> On 5/30/05, JC <[hidden email]> wrote:
>>
>>> Is there a simple way to find out if a value within an array is also
>>> within another array?
>>>
>>> Like this:
>>> Array 1 = 1, 4, 5, 9, 11
>>> Array 2 = 3, 6, 8, 10
>>>
>>> No hit
>>>
>>> Array 1 = 1, 4, 5, 9, 11
>>> Array 2 = 3, 4, 8, 10
>>>
>>> Hit
>>>
>>> Array 1 = 1, 4, 5, 9, 11
>>> Array 2 = 3, 4, 9, 10
>>>
>>> Hit
>>>
>
>
> Regards
>
>
> Mark Palmer, Pageworks
>
> T: 01902 620500            F: 01902 620440
> E: [hidden email]    W: www.pageworks.co.uk
>
>
>
> --
> ------------------------------
> Lasso Support: http://support.omnipilot.com/
> Search the list archives: http://www.listsearch.com/lassotalk.lasso
> Manage your list subscription:
> http://www.listsearch.com/lassotalk.lasso?manage
>


--
------------------------------
Lasso Support: http://support.omnipilot.com/
Search the list archives: http://www.listsearch.com/lassotalk.lasso
Manage your list subscription:  
http://www.listsearch.com/lassotalk.lasso?manage
Reply | Threaded
Open this post in threaded view
|

Re: Find array with an array

jasonhuck
In reply to this post by jc-15
$array->sort;



On 5/30/05, JC <[hidden email]> wrote:
> Aha, that's not so cool. Next question therefor. How do I sort arrays?

--
------------------------------
Lasso Support: http://support.omnipilot.com/
Search the list archives: http://www.listsearch.com/lassotalk.lasso
Manage your list subscription:  
http://www.listsearch.com/lassotalk.lasso?manage
Reply | Threaded
Open this post in threaded view
|

Re: Find array with an array

jc-15
In reply to this post by jc-15
That was so simple I shouldn't have bothered the list. Next time I  
use the manual or LDML site. But thank for helping me and not telling  
me to RTFM... :-)

HDB
JC

30 maj 2005 kl. 16.35 skrev Jason Huck:

> $array->sort;
>
>
>
> On 5/30/05, JC <[hidden email]> wrote:
>
>> Aha, that's not so cool. Next question therefor. How do I sort  
>> arrays?
>>
>
> --
> ------------------------------
> Lasso Support: http://support.omnipilot.com/
> Search the list archives: http://www.listsearch.com/lassotalk.lasso
> Manage your list subscription:
> http://www.listsearch.com/lassotalk.lasso?manage
>


--
------------------------------
Lasso Support: http://support.omnipilot.com/
Search the list archives: http://www.listsearch.com/lassotalk.lasso
Manage your list subscription:  
http://www.listsearch.com/lassotalk.lasso?manage
Reply | Threaded
Open this post in threaded view
|

Re: Find array with an array

Pierre Yelle
In reply to this post by jc-15

[
var:'diff' = ($array1->(difference:$array2));
]

[$diff]


Pierre


On 30-May-05, at 6:26 AM, JC wrote:

> Is there a simple way to find out if a value within an array is also
> within another array?
>
> Like this:
> Array 1 = 1, 4, 5, 9, 11
> Array 2 = 3, 6, 8, 10
>
> No hit
>
> Array 1 = 1, 4, 5, 9, 11
> Array 2 = 3, 4, 8, 10
>
> Hit
>
> Array 1 = 1, 4, 5, 9, 11
> Array 2 = 3, 4, 9, 10
>
> Hit
>
> HDB
> JC
>
> --
> ------------------------------
> Lasso Support: http://support.omnipilot.com/
> Search the list archives: http://www.listsearch.com/lassotalk.lasso
> Manage your list subscription:
> http://www.listsearch.com/lassotalk.lasso?manage
>


--
------------------------------
Lasso Support: http://support.omnipilot.com/
Search the list archives: http://www.listsearch.com/lassotalk.lasso
Manage your list subscription:  
http://www.listsearch.com/lassotalk.lasso?manage
Reply | Threaded
Open this post in threaded view
|

Re: Find array with an array

Fletcher Sandbeck
In reply to this post by jc-15
On 5/30/05 at 4:39 PM by [hidden email] (JC):

>That was so simple I shouldn't have bothered the list. Next time I  
>use the manual or LDML site. But thank for helping me and not telling  
>me to RTFM... :-)

But, if you do you'll discover all the other tags that are available including [Array->Difference] and [Array->Union].

<http://ldml.omnipilot.com/LDMLReference.LassoApp?quick_search=[array>

And, you might even discover that there are other data types that might suit your needs better, like [Set] which generally works like an array, but maintains a unique set of pre-sorted values.

<http://ldml.omnipilot.com/LDMLReference.LassoApp?quick_search=[set>

The Arrays, Maps, and Compound Data Types chapter of the Language Guide contains overviews of the various types and descriptions of when you might want to use each of them.  And, there have been several tips of the week about the different data types as well.

Compound Data Types - <http://www.omnipilot.com/article.lasso?id=7669>
Sets - <http://www.omnipilot.com/article.lasso?id=8231>

I can't imagine programming in Lasso without having the Lasso Reference available.  I've found the simple Dashboard Widget a great way to quickly look up tags.  

<http://www.apple.com/downloads/macosx/dashboard/lassoreference.html>

[fletcher]
--
Fletcher Sandbeck                         [hidden email]
Lasso Product Specialist              [hidden email]
OmniPilot, Software Inc.                http://www.omnipilot.com

--
------------------------------
Lasso Support: http://support.omnipilot.com/
Search the list archives: http://www.listsearch.com/lassotalk.lasso
Manage your list subscription:  
http://www.listsearch.com/lassotalk.lasso?manage
Reply | Threaded
Open this post in threaded view
|

Re: Find array with an array

jc-15
In reply to this post by jc-15
Really lovely!

Set and intersection was precisely what I was in need of.
Such a nice language Lasso is!

HDB
JC

30 maj 2005 kl. 17.25 skrev Fletcher Sandbeck:

> But, if you do you'll discover all the other tags that are  
> available including [Array->Difference] and [Array->Union].
>
> <http://ldml.omnipilot.com/LDMLReference.LassoApp?quick_search=[array>
>
> And, you might even discover that there are other data types that  
> might suit your needs better, like [Set] which generally works like  
> an array, but maintains a unique set of pre-sorted values.
>
> <http://ldml.omnipilot.com/LDMLReference.LassoApp?quick_search=[set>
>


--
------------------------------
Lasso Support: http://support.omnipilot.com/
Search the list archives: http://www.listsearch.com/lassotalk.lasso
Manage your list subscription:  
http://www.listsearch.com/lassotalk.lasso?manage
Reply | Threaded
Open this post in threaded view
|

Re: Find array with an array

jc-15
In reply to this post by jc-15
I discovered Sets. And I like them. But, I looked in the Language  
Guide and I searched the LDMLref but I couldn't find info on how to  
convert an Array into a Set. I have a string that I convert into an  
Array using ->Split. And this I would like to have in a Set instead.  
Ideas?

HDB
JC

30 maj 2005 kl. 17.25 skrev Fletcher Sandbeck:

> And, you might even discover that there are other data types that  
> might suit your needs better, like [Set] which generally works like  
> an array, but maintains a unique set of pre-sorted values.
>


--
------------------------------
Lasso Support: http://support.omnipilot.com/
Search the list archives: http://www.listsearch.com/lassotalk.lasso
Manage your list subscription:  
http://www.listsearch.com/lassotalk.lasso?manage