Copying data from specific lines of a field

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Glenn Boyce
Posts: 137
Joined: Thu Jul 24, 2008 11:22 pm

Copying data from specific lines of a field

Post by Glenn Boyce » Mon Sep 22, 2008 6:31 am

I have the following sort of information in a field

50002
PE/B/PA/EV/PA/B/PE
3 15 PN2020 100

50003
PE/PE/PE
3 15 PN2020 100 13.803808

50004
PE/PE/PE
3 15 PN2020 100 13.803808

I want to find on the first line of each item (the number of lines may vary) and capture the information underneath it.

I have a script which gets the foundline of the item I'm looking for and then gets the next item foundline. I was just going to subtract one foundline from the other and put the lines in between into a global variable. I can find both foundlines and have put each in a temp variable but can't seem to get a result when I subtract one from the other.

Any assistance welcome

Also I have noticed that if I do a find foundline on the field with the same no consecutively that I get a result the first time then no result then a result again ie every second time

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Mon Sep 22, 2008 9:26 am

Hi Glenn,

You really should have posted the relevant part of your script, so we can see what you're talking about.

I'll give it a shot without seeing your script.

Create a stack with two fields. A big field for your data and a small field above it, displaying only one line.

Call the big field "Data" and the small field "Search".

Put the following script into the script of field "Search".

Code: Select all

on enterInField
  returnInField
end enterInField

on returnInField
  if the text of me is a number then
    put lineoffset(the text of me,fld "Data") into myLineNr
    if myLineNr > 0 then
      select line myLineNr to (nextEmptyLine(myLineNr)-1) of fld "Data"
    else
      beep
    end if
  else
    beep
  end if
end returnInField

function nextEmptyLine theLine
  if line theLine of fld "Data" is empty then
    return theLine
  else
    repeat until line theLine of fld "Data" is empty
      add 1 to theLine
    end repeat
  end if
end nextEmptyLine
I hope that this is what you're looking for.

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

Glenn Boyce
Posts: 137
Joined: Thu Jul 24, 2008 11:22 pm

copying specific lines

Post by Glenn Boyce » Mon Sep 22, 2008 9:39 pm

Thanks Mark

Yeah it was a bit dumb really. I've attached the code. You'll see I'm very much an amateur but I'm learning slowly!!

global gfindspec, gstart,glineNo1

Code: Select all

on mouseUp
  put "" into glineNo
  put "" into glineNo1
  put "" into tspec
  put "" into tspec1
  ask "what is Structure Spec No?"
  if it is "" then exit mouseup
  put it into tspec
 
  put tspec + 1 into tspec1

  find tspec in fld "StrucDetail"
  get the foundline -- returns the line number and fld number
  
  set itemdelimiter to space
   put item 2 of it into glineNo -- puts the line number into variable
    --put glineNo
        find tspec1 in fld "StrucDetail"
  get the foundline
  set itemdelimiter to space 
    put item 2 of it into glineNo1 
  
    --put glineno1 - glineNo             note for some reason this doesn't work only returns the value of glineNo1
    repeat with n = 1 to (tlineNo1-tlineNo)
      put line tlineNo of fld "StrucDetail" into  line tlineNo of gfindspec
    end repeat
    
    -- once I have gfindspec it is to be input into a data form
    --put gfindspec
 
end mouseUp
You can see that I have a very basic approach to these things and most of the time I can get things to work - a great advertisement for Revolution and hypercard! I still can't see why it doesn't go but for some reason tlineNo1 and tlineNo don't seem to be holding the line numbers. I changed the foundline to allow me to get an item because the original had getting the 2nd word and I thought that maybe this was a being regarded as text. But somewhere else I have been using the 2nd word of foundline and using it in a calculation so I don't think it's that.

I'll have a try with your code today and see if I can adapt it to suit my needs. Thanks for your assistance

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Mon Sep 22, 2008 11:19 pm

Hi Glenn,

Some simple advice first. Use the fast lineoffset instead of the slow find command. This isn't only more efficient, it also saves you code and is easier to read and understand.

Second, avoid using the it variable if possible. Before you do anything with its value, store it in another variable and use that other variable instead of the it variable. This saves you a lot of potential confusion.

Third, why do you use field names such as "StrucDetail"? Why not a clear and simple "Structure Details"? Surely, you will have to write more code, but it is so much easier to read and understand later.

Your script contains a repeat loop at the end. First of al, tLineNo is not a number. So, it probably causes an execution error. If tLineNo were 0, your script would do nothing. If tLineNo were e.g. 2, you be putting line 2 of one variable into line 2 of another variable for n times. That doesn't make sense. I suppose you want the lines between the first found line and the second found line, but not the second found line itself, to be copied.

Here's my version of your script.

Code: Select all

on mouseUp
   put empty into glineNo
   put empty into glineNo1
   put empty into tspec
   put empty into tspec1
   ask "what is Structure Spec No?"
   if it is not empty then -- using if..end if makes it more readable
      put it into tSpec
      put tSpec + 1 into tSpec1
      --      find tspec in fld "StrucDetail"
      --      get the foundline -- returns the line number and fld number
      --      set itemdelimiter to space
      --      put item 2 of it into glineNo -- puts the line number into variable
      put lineoffset(tSpec,fld "StrucDetail") into gLineNo -- replaces 4 lines above
      --      find tspec1 in fld "StrucDetail"
      --      get the foundline
      --      set itemdelimiter to space
      --      put item 2 of it into glineNo1
      put lineoffset(tSpec1,fld "StrucDetail") into glineNo1 -- replaces 4 lines above
      if gLineNo is 0 or gLineNo1 is 0 then
         beep -- problem occurred
      else
         -- I don' think that tLineNo is a numer and this wouldn't work anyway
         --      repeat with n = 1 to (tlineNo1-tlineNo)
         --         put line tlineNo of fld "StrucDetail" into  line tlineNo of gfindspec
         --      end repeat
         put line gLineNo to gLineNo1 - 1 of fld "StrucDetail" into gFindSpec -- that's all you need
         -- once I have gfindspec it is to be input into a data form
         put gFindSpec
      end if
   end if
end mouseUp
It should be possible to replace your script with this one without any further changes. Let me know if this works (or now).

Best,

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

Glenn Boyce
Posts: 137
Joined: Thu Jul 24, 2008 11:22 pm

Post by Glenn Boyce » Wed Sep 24, 2008 2:45 am

Hi Mark

Thanks for your valuable advice. I didn't know about the lineoffset thing and I took you first version and mixed it with mine to get something pretty close to your second attempt which works just fine. I thought I was being really cool using shortened field names!!! you comments make sense. Thanks for your assistance

Post Reply