Page 1 of 1

Sorting items in a field

Posted: Mon Feb 16, 2009 4:54 am
by Glenn Boyce
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

Posted: Mon Feb 16, 2009 7:09 am
by Janschenkel
How about this?

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
The important things to remember about repeat for each are:
  • 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
So if you're looking to modify the data in a variable or field, the easiest way to use the repeat for each loop to traverse the original data, building a completely new variable with the updated data, and finally replace the original container with the updated data.

HTH,

Jan Schenkel.

Posted: Mon Feb 16, 2009 9:42 am
by Mark
Dear Glenn,

As an alternative, you may try:

Code: Select all

filter myData with "12345 *"
or if you use tab-delimited data:

Code: Select all

filter myData with ("12345" & tab & "*")

Kind regards,

Mark

Posted: Wed Feb 18, 2009 4:20 am
by Glenn Boyce
Thanks Folks

Still stuck

Posted: Wed Feb 18, 2009 11:22 pm
by Glenn Boyce
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: 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

Posted: Wed Feb 18, 2009 11:47 pm
by SparkOut
Hopefully this will help, and the inline comments will be self-explanatory

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
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

Posted: Wed Feb 18, 2009 11:53 pm
by SparkOut

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
You only need to set the itemDelimiter once in the handler, not every iteration through the loop

"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.

Posted: Thu Feb 19, 2009 12:35 am
by Glenn Boyce
Thanks Sparkout