splitting a variable

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
dantomlin
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 43
Joined: Tue Feb 26, 2008 4:07 pm

splitting a variable

Post by dantomlin » Fri Feb 19, 2016 9:57 pm

I have a variable that contains 2 columns and 88 lines. I want to take lines 45 to 88 and put them in column 3 and 4 of the variable so I have 4 columns and only 44 lines?

Is that possible?

Thanks,Dan

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

Re: splitting a variable

Post by dunbarx » Fri Feb 19, 2016 10:24 pm

So much fun.

Are the "columns" coming from a table field?

If so, they are delimited by tabs. Of course, the lines are delimited by returns. The itemDelimiter might be useful here, whatever separates those columns. Item 2 of line 3, that sort of thing...

So I could just give you the answer, (and so could many others here, Grrr... $#@^%$#, God-mn interlopers....

Ahem.

But before that awful turn of events, which you will regret forever (HINT to those "others"), and since you have been with LC for at least a little while, can you write back with a basic plan on how to do this? Maybe even a trial code snippet?

Craig Newman

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: splitting a variable

Post by Simon » Sat Feb 20, 2016 5:02 am

Fun???

Code: Select all

on mouseUp
the code was here
but Craig is a mad hacker
and deleted the answer.
OK so he didn't really
just unhelpful goofing
end mouseUp
:)

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

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

Re: splitting a variable

Post by dunbarx » Sat Feb 20, 2016 7:12 am

@Simon.

Dan has been a member of this forum longer than I have.

@Dan. This really should be fun.

Craig

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

Re: splitting a variable

Post by jacque » Sat Feb 20, 2016 6:25 pm

By the time people post to the forum, they're often stuck and frustrated. Not everyone wants "fun" at that point, and if they are new users trying out the language they may just ditch it and leave. I've seen it happen.

I won't get in your way this time, but I am at a loss why anyone would withhold an answer when asked. People learn just as well from example and are happier for it. What's the point in making them struggle?

Dan, do you want a straight answer? Your call.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: splitting a variable

Post by FourthWorld » Sat Feb 20, 2016 6:34 pm

It's a dangerous thing, me coming across a thread like on a Saturday morning when I have a few spare minutes. My obsession with benchmarking got the better of me once again and I tested four ways of doing this to see which was fastest:

Code: Select all

on mouseUp
  put 1000 into n
  --
  -- Make sample data:
  repeat with i = 1 to 88
    repeat with j = 1 to 2
      put i &"-"&j &tab after tSrc
    end repeat
    put cr into last char of tSrc
  end repeat
  delete last char of tSrc
  --
  -- Test 1: One list, using lineoffset:
  put the millisecs into t
  repeat n
    put SplitAndAppendColumns1(tSrc) into r1 
  end repeat
  put the millisecs - t into t1
  --
  -- Test 2: Split into two lists, using lineoffset:
  put the millisecs into t
  repeat n
    put SplitAndAppendColumns2(tSrc) into r2 
  end repeat
  put the millisecs - t into t2
  --
  -- Test 3: Split into array:
  put the millisecs into t
  repeat n
    put SplitAndAppendColumns3(tSrc) into r3 
  end repeat
  put the millisecs - t into t3
  --
  -- Test 4: This one is totally OCD, like #2 but
  -- using "repeat for each" where we can, on one half:
  put the millisecs into t
  repeat n
    put SplitAndAppendColumns4(tSrc) into r4 
  end repeat
  put the millisecs - t into t4
  --
  -- Display result:
  put t1 && t2 && t3 && t4 \
        &cr& ((r1=r2) AND (r1=r3) AND (r1=r4))
  put r1 into fld 2
end mouseUp


function SplitAndAppendColumns1 pSrc
  put empty into tResult
  repeat with i = 1 to 44
    put line i of pSrc &tab& line (i+44) of pSrc &cr after tResult
  end repeat
  delete last char of tResult -- trailing CR
  return tResult
end SplitAndAppendColumns1


function SplitAndAppendColumns2 pSrc
  put line 1 to 44 of pSrc into tAList
  put line 45 to 88 of pSrc into tBlist
  repeat with i = 1 to 44
    put line i of tAlist &tab& line i of tBlist &cr after tResult
  end repeat
  delete last char of tResult
  return tResult
end SplitAndAppendColumns2


function SplitAndAppendColumns3 pSrc
  split pSrc with cr
  repeat with i = 1 to 44
    put pSrc[i] &tab& pSrc[i+44] &cr after tResult
  end repeat
  delete last char of tResult
  return tResult
end SplitAndAppendColumns3


function SplitAndAppendColumns4 pSrc
  put line 1 to 44 of pSrc into tAList
  put line 45 to 88 of pSrc into tBlist
  put 0 into i
  repeat for each line tLine in tAlist
    add 1 to i
    put tLine &tab& line i of tBlist &cr after tResult
  end repeat
  delete last char of tResult
  return tResult
end SplitAndAppendColumns4
Most interesting discovery: run the rest with n as 100, then with 1000, then with 10000 and observe the results.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: splitting a variable

Post by dunbarx » Sat Feb 20, 2016 7:56 pm

Jacque.

This is the only subject upon which we ever butt heads.

Moot, now, because Richard has provided several examples. He couldn't help himself. :D I suppose neither can I. 8)

But will Dan examine his solutions so that he does indeed learn something? Or will he drop one handler or another into his project and just go?

You must recall that oftentimes I do indeed just supply a solution, but exhort the OP to examine in detail what I sent, and provide feedback (as either payment, or via my annoying "homework" peccadillo). They rarely do. This is not shocking, they are as lazy as I am.

You just commented on a one-liner in the beginners section. I gave that because even one reading of it should provide adequate instruction. Your comment was spot on, and a good alternative, assuming that the lesson was learned. The stuff Richard gave, well, I bet it will not engender an hour or two of critical examination, and I think this will be less useful overall to Dan than if he had the patience to deal with a curmudgeon for few days of back and forth.

Dan?

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

Re: splitting a variable

Post by jacque » Sat Feb 20, 2016 8:28 pm

Yes, we disagree, and that's okay. I also know you provide more answers here than provocation, which is great. But this particular topic hits my buttons. Sorry.

I have to ask, why is it our responsibility to ensure they learn anything? Remember, HC supplied scripted buttons and fields users could just drop on a stack and go on. Why does this forum have to be a classroom? So what if they don't examine the scripts? It's nothing to me.

When it becomes important to the user to learn the details, they will. I don't question their motivation. If I know the answer I tell them, and they can do whatever they want with it. I see my role as helper, not teacher.

My main concern is that we not scare off or frustrate anyone, and forcing someone into student mode against their will can do that. That would make me very sad.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: splitting a variable

Post by dunbarx » Sat Feb 20, 2016 9:09 pm

Jacque.

Code: Select all

That would make me very sad.
I have already had an xTalk death in my family, and do not want another. That would make me very sad. My motivation is to expand the user base, to keep those we already have, and empower those who want to learn. I want all other languages to die a horrible death.

I like to teach "students" more than I like to solve their problems. Well, that is mostly true. I am aware that some people are on a track, and just need technical advice. Those (if I discern the difference correctly) I just try to give an example, though I do ask for feedback and some form of additional effort. But those that either ought to know better, or are just starting, or submit english-like but outlandish LC gibberish, should not be mollycoddled, but rather engaged, with the warning that it will take time, work and commitment. And the promise that it will be more than worth it, and, ahem, fun. Otherwise, neither of us should bother.

So.

Dan, how are we doing?

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: splitting a variable

Post by FourthWorld » Sat Feb 20, 2016 9:16 pm

At the risk of taking this thread even further away from its original topic (I can move these posts to a new thread if you folks think it's a good idea), if there's anything I've learned over the years as both an instructor and learner of programming languages it's that there are many different learning modes, and some work well for some folks and other modes for others.

For myself, one of the biggest boons to my education came from dissecting the sample projects included with HyperCard and SuperCard (SC's SampleDraw was particularly inspiring!). I've found that sample code like that gives me the best of both learning from the documentation and learning from example: seeing finished working examples lets me see the end result in action, which is very motivating. And as I dissect the code I can look up commands and functions that are new to me in the documentation to learn more about how I can use them in other code.

I see value in both of the learning styles you two advocate, and there are others. One of the good things about the diversity of this community is that we can offer that breadth, and hopefully among the various options presented the OP can find at least one that resonates with them.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: splitting a variable

Post by jacque » Sat Feb 20, 2016 10:29 pm

But those that either ought to know better, or are just starting, or submit english-like but outlandish LC gibberish, should not be mollycoddled, but rather engaged, with the warning that it will take time, work and commitment.
I think that's where you'll lose many of them. "Well, if it's that hard, forget it." And, to be honest, I don't think we can decide whether a user should know better. They know what they know.

I think I mentioned once that I tried to learn C and it took me two very frustrating days to write a single line of code that would compile. I didn't understand the punctuation, the syntax, or the conventions. My one line of code was about 25 characters long. Two days. When it was time to write the next line, I couldn't face it so I found a place that seemed friendly enough to ask questions. They said "do it like this". That was so much more gratifying and productive than another two days of flat-out anger and, frankly, increasing hatred of the language. If they had told me to go study and dig around some more, I'd have thrown up my hands and stopped right there. It just wasn't worth it. I'd had enough, and no promise of future reward was going to put me through that again.

After a couple of months I learned enough to write simple code if I was careful. I eventually gave it up because LC was so much easer for me. But I wouldn't have made it past those first two days without concrete examples.

My vivid recollection of just how painful it was to learn on my own is what makes me so stubborn about our approach here in the forums. Richard is right that we all have different learning styles, but since I can't know what a user needs, I play it safe and just answer whatever is asked. Maybe some people would prefer another style; it would be interesting to know how that breaks out. I do remember at least one person who told you they didn't want the answer, just some hints, so your way does seem to appeal to some.

(Richard, feel free to move this if you like, we're going off-track.)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: splitting a variable

Post by sritcp » Sat Feb 20, 2016 11:03 pm

As someone who has benefited from this forum and as someone still learning LC (I am familiar with less than 5% of the dictionary entries!), here's how I'd like to be helped:

1. Provide a solution to the specific problem, preferably correcting the code I have used (unless my code is way out in the left field).

2. Provide alternative solutions to a generalized version of my specific problem.

3. Make general comments relating to my problem/solution (e.g., as a general rule, use "repeat for each..." where you can, in preference to "repeat with i=...", etc.)

4. In cases where my structuring of the problem is the culprit, not knowledge of the language, say so and provide a fresh model complete with code.

Obviously, this is achieved over a number of comments by different LC experts, each adding a different dimension.
This way, I will have my immediate problem solved, but leave the thread with a greater appreciation of (and thirst for) LiveCode.

Regards,
Sri

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

Re: splitting a variable

Post by dunbarx » Sat Feb 20, 2016 11:25 pm

Richard.

What Jacque said. This thread ought to bring other opinions.

I would hate to lose even one possible newbie that way. Which is why I try to be light and wry, if not actually fun. But I see what you mean, always have, you know.

I will be more giving. I think I said that before. And thereby more demanding of that repayment in kind. But I rarely get any at all.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: splitting a variable

Post by FourthWorld » Sun Feb 21, 2016 12:34 am

As I look at the posts here more closely it's not entirely clear which ones I should move, since many have references to things which should remain yet if a post that refers to them is moved it makes less sense.

I'll leave them all in place for now unless someone has a clear idea of what should be moved.

If you want to start a new topic on this feel free, perhaps under "Off Topic" (which is where I was going to move this anyway since all the other sections are for LC coding discussions).
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: splitting a variable

Post by dunbarx » Sun Feb 21, 2016 4:07 am

Back on topic.

Dan, check out this handler.

Code: Select all

on mouseUp
   repeat with y = 1 to 88 --build 88 line data set
      put ("A" & y) & tab & ("B" & y) into line y of temp
   end repeat

   set the itemDel to tab
   repeat with x = 45 to 88 --load the bottom half onto the top half
      put tab & line x of temp after line (x-44) of temp
   end repeat
delete line 45 to 88 of temp
end mouseUp
So, what do you think would be a way to make this more general, in that the hard coded "88", "45", etc, might be user settable. Note that ordinarily, I think, the gadget that builds the data set is not required, that it exists somewhere already, No? Let me know what you think.

Craig

Post Reply