Sorting numbers by mathematicals properties

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Sorting numbers by mathematicals properties

Post by jmburnod » Tue Apr 23, 2013 11:19 pm

Hi Matthieu,
When tNumber don't find equal tNumber it return empty and then the value ?
The script put tNumber & "," after line tNumber
"Filter without empty" mean "remove empty" ?
Yes Its remove all empty lines (there is 2449 empty lines before the first filled line
All the best
Jean-Marc
https://alternatic.ch

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Sorting numbers by mathematicals properties

Post by dunbarx » Tue Apr 23, 2013 11:32 pm

Matthieu.

You are correct.

The small problem I first mentioned is that when you have three or more lines that are the same, the resulting list will contain an extra line wherever this occurs. If you step through the script, you will see this happen.

The way out is to remove duplicate lines. That is, if you use this method. I think you should play with it for a while, because it will show you several things about loops, and answer your questions about what the counters are for with that type of repeat construction.

If you had a list:

10
20
30
30
30
40
50
50
60

Your script would return: 3,4,4,5,7,8. The "4" is a duplicate. Do you see? The list should be 3,4,5,7,8. Step through like I said. And try to correct the script to remove those duplicate lines.

Craig Newman

matgarage
Posts: 73
Joined: Sat Apr 20, 2013 11:39 am

Re: Sorting numbers by mathematicals properties

Post by matgarage » Wed Apr 24, 2013 6:08 pm

Hi Jean-Marc and dunbarx,

Your right, I need to play now with your script with differents options to well understand the process.

put "Let's Go!' into now
:)

Thank you again

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Sorting numbers by mathematicals properties

Post by jacque » Wed Apr 24, 2013 6:27 pm

I like arrays for this sort of thing but it may be more than you want to learn right now. Here's my solution though. The only difference between multiples and common divisors is that the "multiples" option also counts the lines that have no matches.

Code: Select all

on mouseUp
  put fld 1 into tValues
  put getMatches(tValues,"equal") into fld "equals"
  put getMatches(tValues,"divisor","500,1000,1500") into fld "divisors"
  put getMatches(tValues,"multiples","2500") into fld "multiples"
end mouseUp

function getMatches pValues,pType,pDivisors
  switch pType -- "equal","divisor"
    case "equal"
      repeat for each line l in pValues
        put word 1 of l & comma after tMatches[word 2 of l]
      end repeat
      break
    case "divisor"
      repeat for each line l in pValues
        repeat for each item i in pDivisors
          if word 2 of l mod i = 0 then
            put word 1 of l & comma after tMatches[i]
          end if
        end repeat
      end repeat
      break
    case "multiples"
      repeat for each line l in pValues
        repeat for each item i in pDivisors
          if word 2 of l mod i = 0 then
            put word 1 of l & comma after tMatches[i]
          else
            put word 1 of l & comma after tMatches["none"]
          end if
        end repeat
      end repeat
      break
  end switch
  combine tMatches by cr and tab
  return tMatches
end getMatches
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

matgarage
Posts: 73
Joined: Sat Apr 20, 2013 11:39 am

Re: Sorting numbers by mathematicals properties

Post by matgarage » Wed Apr 24, 2013 6:40 pm

Hi jacque,

I will put your script in my laboratory stack and will try to understand it.
Even if it's only a part of it.

More is better in some case...

I'll come back with more questions now :wink:

Thank you

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Sorting numbers by mathematicals properties

Post by jacque » Wed Apr 24, 2013 6:58 pm

Questions are good, everyone learns from them. :)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

matgarage
Posts: 73
Joined: Sat Apr 20, 2013 11:39 am

Re: Sorting numbers by mathematicals properties

Post by matgarage » Thu Apr 25, 2013 8:06 pm

Hi Craig,
I've tried this

Code: Select all

on mouseUp
   put field dataP into yourData
   sort yourData numeric
   repeat with y = 1 to the number of lines of yourData
         if line y of yourData = line (y + 1) of yourData then put y & "=" & (y + 1) & "," after equalList
   if line y of yourData <> line (y + 1) of yourData then put cr after equalList
   end repeat
   answer equalList
end mouseUp
Now my result is more readable. By the way I understand a little more how does loops work.
1=2,2=3,
4=5,
6=7,7=8,
The need of numeric sorting too.

I've tried to get a result like 1=2=3.
Without succes
I think I need to loop until y<>y+1 but can't find how. I'm feeling so stupid sometimes...

An advice ?

Regards

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Sorting numbers by mathematicals properties

Post by jmburnod » Fri Apr 26, 2013 8:57 am

Hi Matthieu,

A quick and dirty solution

Code: Select all

on mouseUp
   put "10,20,30,30,30,40,50,50,60," into yourData
   replace "," with cr in yourData
    repeat with y = 1 to the number of lines of yourData
      put line y of yourData into tNum
      put y & "=" after line tNum of equalList
      end repeat
   filter equalList without empty
   put the itemdel into tOID
   set the itemdel to "="
   put empty into equalList2
   repeat for each line tLine in equalList
      if the num of items of tLine = 1 then next repeat
      put char 1 to -2 of tLine & cr after equalList2
   end repeat
   delete char -1 of equalList2
   answer equalList2
end mouseUp
Best regards
Jean-Marc
https://alternatic.ch

matgarage
Posts: 73
Joined: Sat Apr 20, 2013 11:39 am

Re: Sorting numbers by mathematicals properties

Post by matgarage » Mon May 06, 2013 11:31 am

Hi all,

I've played a lot with your script by testing differents options and targets.
I've understand now how loops works. I think.
Sorry jaque, I think your right, I will keep your script for later. Too much for me... for now.

I would want to go ahead now by managing tha datas and dispatching them to separate targets.
I've created a Datagrid with 2 columns (colProd an colTir) that I populate by 2 text fields (addProd and addTir) with this button script :

Code: Select all

on mouseUp
   put field addProd into Prod
   put field addTir into Tir
   put Prod into theDataA["ColProd"]
   put Tir into theDataA["ColTir"]
   put  1 into theLineNo
   dispatch "AddData" to group "listProd" with theDataA, theLineNo
end mouseUp 
Now I want to sort this data grid by dispaching them with the equality or common divisor script.
The goal is to add the sorted value and/or the product name in a particular object (text field or datagrid) for each equality , for exemple.
I can't find how to apply the script to column items (sorting) and then return another content of the line.
colProd,colTir
p1,2000
p2,2000
p3,3000
p4,4000

result :
group 2000 : p1,p2
group 3000 : p3
group 4000 : p4


I've tried this but I think it's not the good way :

Code: Select all

on mouseUp 
   put the dgText of group "listProd" into quantTir
   repeat with y=1 to the number of lines of quantTir
      delete word 1to 2 of line y of quantTir
   end repeat
   sort quantTir numeric
   repeat with z = 1 to the number of lines of quantTir
      if line z of quantTir = line z+1 of quantTir then put z & cr & z+1 & cr into field qt1
   end repeat
end mouseUp
Could you help me with this problem ?

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Sorting numbers by mathematicals properties

Post by Klaus » Mon May 06, 2013 12:16 pm

Hi Mat,

you can use an array for this :-)
In my example field 1 =
p1,2000
p2,2000
p3,3000
p4,4000

We use ITEM 2 of each line as the KEY for our array, and ITEM 1 of each line as the CONTENT,
that we APPEND with a COMMA:

Code: Select all

on mouseUp
  put fld 1 into tList
  put empty into tArray
  repeat for each line i in tList
    put item 1 of i into tContent
    put item 2 of i into tKey
    put tContent & "," after tArray[tKey]
  end repeat
  ## ->  tArray:
  ## 2000[p1,p2,]
  ## 3000[p3,]
  ## 4000[p4,]

  ## Now we extract the content and display the KEYS and the CONTENT of the array!
  ## You may want to remove the trailing COMMAS ;-)
  repeat for each key tKey2 in tArray
    put tKey2 & ":" && tArray[tKey2] & CR after tNewList
  end repeat
  put tNewList into fld 2
  ## 2000: p1,p2,
  ## 3000: p3,
  ## 4000: p4,
end mouseUp
Tested and works :-)

Best

Klaus

matgarage
Posts: 73
Joined: Sat Apr 20, 2013 11:39 am

Re: Sorting numbers by mathematicals properties

Post by matgarage » Mon May 06, 2013 1:58 pm

Hi Klaus,

So it's now time for me to working with array.
( jaque, I will back to your script earlier than expected :) )

Thank you Klaus.
You use a text field for the data. But what about Data Grid ?
If I put :

Code: Select all

put the dgText of group "listProd" into tList
instead of

Code: Select all

put fld qt4 into tList
listProd is my data grid and qt4 a text field with the data text in csv.
It's not working.

Could you tell me why ?

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Sorting numbers by mathematicals properties

Post by sturgis » Mon May 06, 2013 2:13 pm

when you get the dgtext from a datagrid, it returns a cr and TAB delimited list not commas.

To get around this, (without looking too deeply at Klaus script) if you "set the itemdelimiter to tab" at the beginning of the handler it will probably work. The default itemdelimiter is comma, if you need to work with a different delimiter for items you must set it in the handler where it will be used.

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Sorting numbers by mathematicals properties

Post by Klaus » Mon May 06, 2013 2:18 pm

Hi matgarage,

sorrs, looks like I misunderstood you.

Sure you can also use a datagrid, depends on what is in the datagrid :D
As sturgis said, "the dgtext of grp datagrid" is TAB and CR delimited text.

You you can simply add "set itemdel to TAB" right after the "mouseup" and
it should work with your datagrid data, presuming the data is something like this:
p1 TAB 2000
p2 TAB 2000
p3 TAB 3000
p4 TAB 4000
...



Best

Klaus

matgarage
Posts: 73
Joined: Sat Apr 20, 2013 11:39 am

Re: Sorting numbers by mathematicals properties

Post by matgarage » Mon May 06, 2013 2:52 pm

Nice, it's working now :)
Thank you sturgis and Klaus
You've just open a new door in my learning of the basics of Livecode.

matgarage
Posts: 73
Joined: Sat Apr 20, 2013 11:39 am

Re: Sorting numbers by mathematicals properties

Post by matgarage » Tue May 07, 2013 4:27 pm

Hi,
Now I would want to adress each lines of tArray to a different field.
I've tried that

Code: Select all

repeat for each key tKey2 in tArray
          put tKey2 into fld qp1
      put tArray[tKey2] & CR after tNewList
     end repeat
   
   repeat with newL = 1 to the number of lines in tNewlist
put line newL of tNewlist & cr after transList
put transList into fld "proP[newL]"
end repeat
In the second loop I've tried to modify the name of the traget by the line index.
It's not working.
What's wrong ?

Post Reply