Putting a number in correct position of ascending numbers

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

Post Reply
montymay
Posts: 145
Joined: Thu Jul 18, 2013 5:23 am

Putting a number in correct position of ascending numbers

Post by montymay » Tue Nov 17, 2015 7:37 am

I need a script that will insert a user-supplied number, and some data associated with it, from one field into the correct position of an ascending list of numbers with data into another field, e.g., inserting:

500:
AG Opinion:
98-0333 DRT Gross Receipts Taxes


between 414: and 1013: in this list:
  • 411:
    AG Opinion:
    71-0006 GOV Custody of Great Seal of Guam

    414:
    AG Opinion
    88-0316 GSA Advertising Coupons on Guam Customs Forms 411

    [new data goes here]

    1013:
    AG Opinion
    70-0004 GOV Liberation Day
I am not a trained programmer, but I am thinking that it requires lots of code using the repeat control structure and the operators => and <=, and that there is a standard algorithm in any language, but I don't know where to begin. If providing the actual code is too time consuming, could any advanced programmer out there explain a little bit about the method? Thanks for any tips.

Monty

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm

Re: Putting a number in correct position of ascending number

Post by zaxos » Tue Nov 17, 2015 12:05 pm

Not sure if i got this right but its worth a try.
No repeat needed, lineoffset should do the trick. Assuming field 1 is the field you want to inset and field 2 the field you want to insert into then:

Code: Select all

Put field 1 into line(lineoffset("414:",field 2)+2) of field 2
What this does is find the number of the line 414: belongs to, lets say it is line 505, add 2 to it since the next 2 lines are data associated with "414:", 505 is now 507 so (lineoffset("414:",field 2)+2) returns 507 and now we put our data into line 507 of field 2...
If i were you i would use an array, all you have to do is insert the data put it into a datagrid and then sort it out by a column, lets say column "ID" witch contains the ID (413,414,500...) if you need help with it let me know.
Knowledge is meant to be shared.

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

Re: Putting a number in correct position of ascending number

Post by dunbarx » Tue Nov 17, 2015 3:06 pm

Hi.

What zaxos said, provided that the data format for both the target field and the user input are fixed. This is rarely the case, especially when people type into a container. They are often filled with uncontrolled returns, which alter the number of lines in an instance of a dataset.

Where is this data derived from? By that I mean can a special (perhaps untypable) character be included in both the target data and the user data? This character can be appended when whatever process you use to insert the new text is run. This special character (could even be an invisible character) allows a unique way to parse the data into distinct blocs, called "items" in LC parlance. The resulting code would then no longer depend on the structure of the text at all. (pseudo):

Code: Select all

get userInput
put specialChar after userInput
put userInput after theTargetText
set the itemDelimiter to specialChar
sort theTargetText numeric
Something to think about. Robustness should be considered as early as possible.

Craig Newman

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: Putting a number in correct position of ascending number

Post by sritcp » Tue Nov 17, 2015 4:07 pm

Hi montymay:

Here's a general approach that you can modify:

1. Put your data into an array and store it as a global or custom property

Code: Select all

global gDBArray

/* Do the following for each record tRecord*/

put line 1 of tRecord into tKey
delete the last char of tKey
repeat with i=1 to 3
put line i of tRecord into gDBArray[tKey][i]
end repeat
2. Do the same thing for the new record that you want to add

3. Display the data in a field

Code: Select all

put the keys of gDBArray into theKeys
sort lines of theKeys ascending numeric
put empty into field "DisplayField"
repeat with i =1 to the number of lines in theKeys

   put line i of theKeys into tKey
   repeat with j = 1 to 3
      put gDBArray[tKey][j] & return after field "DisplayField"
   end repeat
   put return after field "DisplayField" -- insert an empty line between records

end repeat
Regards,
Sri

Post Reply