Sorting items in a field
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 137
- Joined: Thu Jul 24, 2008 11:22 pm
Sorting items in a field
I have the following items in a field:
12345 Burly Boy pack 200 ctn 4 2/2/09 800 bags 6.75 675 Auckland Nth Shore A/F/2
12345 Burly Boy Pack 200 ctn 2 2/2/09 400 bags 6.75 675 Auckland Nth Shore A/F/4
456222 Bluebird Chip Wrapper 150gm 28/1/09 3/3/09 25000 M 3.50 87500 Auckland Manukau G/C/25
556227 Fonterra butter wrap 16/2/09 125000 M 4.52 565000 Auckland Manukau G/C/25
223548 Waltons Outdoor Tub Mix 40L 08 15/1/09 65000 M 3.56 231400 Auckland Manukau H/C/34
The first word is a part number and it may occur many times in the field. I want to be able to find and retrieve all the lines that have a particular part number and put them in another field where I will total up some of the line items etc. I am having trouble building a repeat routine to select only the items relating to a specified part Number.
Any assistance greatly appreciated
12345 Burly Boy pack 200 ctn 4 2/2/09 800 bags 6.75 675 Auckland Nth Shore A/F/2
12345 Burly Boy Pack 200 ctn 2 2/2/09 400 bags 6.75 675 Auckland Nth Shore A/F/4
456222 Bluebird Chip Wrapper 150gm 28/1/09 3/3/09 25000 M 3.50 87500 Auckland Manukau G/C/25
556227 Fonterra butter wrap 16/2/09 125000 M 4.52 565000 Auckland Manukau G/C/25
223548 Waltons Outdoor Tub Mix 40L 08 15/1/09 65000 M 3.56 231400 Auckland Manukau H/C/34
The first word is a part number and it may occur many times in the field. I want to be able to find and retrieve all the lines that have a particular part number and put them in another field where I will total up some of the line items etc. I am having trouble building a repeat routine to select only the items relating to a specified part Number.
Any assistance greatly appreciated
-
- VIP Livecode Opensource Backer
- Posts: 977
- Joined: Sat Apr 08, 2006 7:47 am
- Contact:
How about this?
The important things to remember about repeat for each are:
HTH,
Jan Schenkel.
Code: Select all
repeat for each line tLine in tData
if word 1 of tLine is "12345" then
-- do your thing
end if
end repeat
- it is the fastest way to go through data, line per line, item per item, etc.
- do not tamper with the contents of the labelVariable during the repeat loop
- do not tamper with the contents of the container during the repeat loop
HTH,
Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com
www.quartam.com
Dear Glenn,
As an alternative, you may try:
or if you use tab-delimited data:
Kind regards,
Mark
As an alternative, you may try:
Code: Select all
filter myData with "12345 *"
Code: Select all
filter myData with ("12345" & tab & "*")
Kind regards,
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
-
- Posts: 137
- Joined: Thu Jul 24, 2008 11:22 pm
Still stuck
I tried the first option but there is something wrong with my syntax as it doesn't go and I've spent and hour trying to figure out why!!
code is:
code is:
Code: Select all
on mouseUp
ask "What is Part No?" with Cancel and OK
put it into tpartno
put tpartNo
put the number of lines of fld "finishedGoods" into tNoLines
put "0" into tlinecount
repeat for each line tline in fld "finishedgoods"
set itemdelimiter to tab
if item 1 of line tline of fld "finishedGoods" is tpartNo then
add 1 to tlinecount
put line tline of fld "finishedgoods" into line tlinecount of tresult
end if
end repeat
put tresult into fld "result"
end mouseUp
Hopefully this will help, and the inline comments will be self-explanatory
Bleagh, that's a mess. It looks better if you put it in the script editor in the IDE and have it coloured and formatted. meh... repeating below for the sake of clarity
Code: Select all
on mouseUp
local tpartno, tline, tresult
ask "What is Part No?" with "Cancel" and "OK"
put it into tpartno
set the itemDelimiter to tab
--you only need to set the itemDelimiter once in the handler, not every iteration through the loop
repeat for each line tline in fld "finishedgoods"
-- repeat for each substitutes each "chunk" in question into the variable declared
-- every iteration of the loop. So tline becomes the *whole* of line 1, then the whole of line 2, etc
-- that means you can do away with counting the lines as you go. If you *were* going to use
-- a count loop, you would do it more easily with "repeat with tlineNo = 1 to the number of lines of field "finishedgoods"
if item 1 of tline is tpartNo then
put tline & cr after tresult
end if
-- as an aside, if you have a recent version of Rev (2.9 onwards I think, maybe 2.8 as well, not sure)
-- you can use "if tline begins with tpartno" and avoid worrying about the itemDelimiter altogether
end repeat
delete the last char of tresult --tidy the last trailing cr
put tresult into fld "result"
end mouseUp
Code: Select all
on mouseUp
local tpartno, tline, tresult
ask "What is Part No?" with "Cancel" and "OK"
put it into tpartno
set the itemDelimiter to tab
repeat for each line tline in fld "finishedgoods"
if item 1 of tline is tpartNo then
put tline & cr after tresult
end if
end repeat
delete the last char of tresult --tidy the last trailing cr
put tresult into fld "result"
end mouseUp
"repeat for each" substitutes each "chunk" in question into the variable declared every iteration of the loop. So tline becomes the *whole* of line 1, then the whole of line 2, etc. That means you can do away with counting the lines as you go. If you *were* going to use a count loop, you would do it more easily with "repeat with tlineNo = 1 to the number of lines of field "finishedgoods"
As an aside, if you have a recent version of Rev (2.9 onwards I think, maybe 2.8 as well, not sure) you can use "if tline begins with tpartno" and avoid worrying about the itemDelimiter altogether.