Help with Arrays

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

Help with Arrays

Bill Vlahos
I have a list of names many of which repeat and I want to get a count of how many times each name appears in the list and sort it.

This seems like a perfect example of using an array but I'm struggling to figure it out.

Sample list:
        Bill
        Mary
        Bill
        John
        Jim
        Jane
        Bill
        Jim
        Jane

The popularity result should look like this:
        Bill (3)
        Jane (2)
        Jim (2)
        John (1)
        Mary (1)

How would I do this using an array?

Thanks,
Bill Vlahos
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Help with Arrays

Phil Davis-5
Hi Bill,

Here is a clunky and simplistic way that works:

on mouseUp
     -- get the number of occurrences for each name
     put myList() into tList
     repeat for each line tLine in tList
         add 1 to tCountsByName[tLine]
     end repeat

     -- reformat the 'counts' data
     combine tCountsByName with CR and "="
     sort lines of tCountsByName word 1 to -2 of each -- the minor sort
     sort lines of tCountsByName numeric descending by last word of each
-- the major sort
     replace "=" " (" in tCountsByName
     replace CR with ")" & CR in tCountsByName
     put ")" after tCountsByName

     -- you're done!
     answer tCountsByName
end mouseUp


function myList
     get "Bill,Mary,Bill,John,Jim,Jane,Bill,Jim,Jane"
     replace comma with CR in it
     return it
end myList


HTH -
Phil Davis


On 10/14/13 1:52 PM, Bill Vlahos wrote:

> I have a list of names many of which repeat and I want to get a count of how many times each name appears in the list and sort it.
>
> This seems like a perfect example of using an array but I'm struggling to figure it out.
>
> Sample list:
> Bill
> Mary
> Bill
> John
> Jim
> Jane
> Bill
> Jim
> Jane
>
> The popularity result should look like this:
> Bill (3)
> Jane (2)
> Jim (2)
> John (1)
> Mary (1)
>
> How would I do this using an array?
>
> Thanks,
> Bill Vlahos
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>

--
Phil Davis


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Help with Arrays

Phil Davis-5
Oops. See below.

On 10/14/13 2:16 PM, Phil Davis wrote:

> Hi Bill,
>
> Here is a clunky and simplistic way that works:
>
> on mouseUp
>     -- get the number of occurrences for each name
>     put myList() into tList
>     repeat for each line tLine in tList
>         add 1 to tCountsByName[tLine]
>     end repeat
>
>     -- reformat the 'counts' data
>     combine tCountsByName with CR and "="
>     sort lines of tCountsByName word 1 to -2 of each -- the minor sort
should be:
         sort lines of tCountsByName by word 1 to -2 of each -- the
minor sort

> sort lines of tCountsByName numeric descending by last word of each --
> the major sort
>     replace "=" " (" in tCountsByName
>     replace CR with ")" & CR in tCountsByName
>     put ")" after tCountsByName
>
>     -- you're done!
>     answer tCountsByName
> end mouseUp
>
>
> function myList
>     get "Bill,Mary,Bill,John,Jim,Jane,Bill,Jim,Jane"
>     replace comma with CR in it
>     return it
> end myList
>
>
> HTH -
> Phil Davis
>
>
> On 10/14/13 1:52 PM, Bill Vlahos wrote:
>> I have a list of names many of which repeat and I want to get a count
>> of how many times each name appears in the list and sort it.
>>
>> This seems like a perfect example of using an array but I'm
>> struggling to figure it out.
>>
>> Sample list:
>>     Bill
>>     Mary
>>     Bill
>>     John
>>     Jim
>>     Jane
>>     Bill
>>     Jim
>>     Jane
>>
>> The popularity result should look like this:
>>     Bill (3)
>>     Jane (2)
>>     Jim (2)
>>     John (1)
>>     Mary (1)
>>
>> How would I do this using an array?
>>
>> Thanks,
>> Bill Vlahos
>> _______________________________________________
>> use-livecode mailing list
>> [hidden email]
>> Please visit this url to subscribe, unsubscribe and manage your
>> subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>>
>

--
Phil Davis


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Help with Arrays

mwieder
In reply to this post by Bill Vlahos
Bill Vlahos <bvlahos@...> writes:

> How would I do this using an array?

Well, Phil beat me to it, but I was going to suggest much the same thing.
That repeat loop is what does the trick.

Of course, the real-world answer probably depends on where the list of names
is coming from... if it's in a mongodb database, just issue an aggregate
command and get your results much faster with a one-liner than anything you
could think of doing in a script.

--
 Mark Wieder
 [hidden email]



_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
--
 Mark Wieder
 ahsoftware@gmail.com
Reply | Threaded
Open this post in threaded view
|

Re: Help with Arrays

Andrew Kluthe-2
I wish we had a mongodb native driver with aggregate queries availiable.
::drools::


On Mon, Oct 14, 2013 at 7:25 PM, Mark Wieder <[hidden email]> wrote:

> Bill Vlahos <bvlahos@...> writes:
>
> > How would I do this using an array?
>
> Well, Phil beat me to it, but I was going to suggest much the same thing.
> That repeat loop is what does the trick.
>
> Of course, the real-world answer probably depends on where the list of
> names
> is coming from... if it's in a mongodb database, just issue an aggregate
> command and get your results much faster with a one-liner than anything you
> could think of doing in a script.
>
> --
>  Mark Wieder
>  [hidden email]
>
>
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>



--
Regards,

Andrew Kluthe
[hidden email]
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Help with Arrays

mwieder
Andrew-

Monday, October 14, 2013, 5:45:15 PM, you wrote:

> I wish we had a mongodb native driver with aggregate queries availiable.
> ::drools::

In my spare time *someday*... yeah, the commandline-driven interface
still leaves something to be desired.

--
-Mark Wieder
 [hidden email]


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
--
 Mark Wieder
 ahsoftware@gmail.com
Reply | Threaded
Open this post in threaded view
|

Re: Help with Arrays

Dave Kilroy
That's strange - the sort command doesn't make a difference when I try it out (LC 6.1.2 on Mac 10.8.5) - is it working for others?

Dave

"The first 90% of the task takes 90% of the time, and the last 10% takes the other 90% of the time."
Peter M. Brigham
Reply | Threaded
Open this post in threaded view
|

Re: Help with Arrays

Peter Haworth
In reply to this post by mwieder
Or on SQL

SELECT name,count(name) FROM names GROUP BY name ORDER BY name

Pete
lcSQL Software
On Oct 14, 2013 9:25 PM, "Mark Wieder" <[hidden email]> wrote:

> Bill Vlahos <bvlahos@...> writes:
>
> > How would I do this using an array?
>
> Well, Phil beat me to it, but I was going to suggest much the same thing.
> That repeat loop is what does the trick.
>
> Of course, the real-world answer probably depends on where the list of
> names
> is coming from... if it's in a mongodb database, just issue an aggregate
> command and get your results much faster with a one-liner than anything you
> could think of doing in a script.
>
> --
>  Mark Wieder
>  [hidden email]
>
>
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Help with Arrays

dunbarxx
In reply to this post by Dave Kilroy
Dave.


If what you are asking is being able to keep a sort order when making an array, or extracting an array back into a normal variable, then know that this is not how LC manages them.


Arrays in LC are purely associative; sort order is not even glanced at. All sorting has to be done once the array is changed back into the clear, usually at the time the "combine" command does its thing.



Craig Newman


-----Original Message-----
From: Dave Kilroy <[hidden email]>
To: use-revolution <[hidden email]>
Sent: Thu, Oct 17, 2013 10:52 am
Subject: Re: Help with Arrays


That's strange - the sort command doesn't make a difference when I try it out
(LC 6.1.2 on Mac 10.8.5) - is it working for others?

Dave





-----
"Some are born coders, some achieve coding, and some have coding thrust upon
them." - William Shakespeare & Hugh Senior

--
View this message in context: http://runtime-revolution.278305.n4.nabble.com/Help-with-Arrays-tp4671031p4671056.html
Sent from the Revolution - User mailing list archive at Nabble.com.

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

 

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Help with Arrays

Dave Kilroy
Thanks Craig

DOH! Yes I was trying to sort an array...
"The first 90% of the task takes 90% of the time, and the last 10% takes the other 90% of the time."
Peter M. Brigham