transpose a variable of data

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

transpose a variable of data

Post by dantomlin » Fri Oct 09, 2015 7:46 pm

I have a variable that has 4 lines data with 10 columns across. I want to transpose that data into 10 lines with 4 columns similar to the way you can do it excel thru the paste special commands.

Can anyone get me hints on how to write the code for this with an example?

Thanks, Dan

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

Re: transpose a variable of data

Post by dunbarx » Fri Oct 09, 2015 8:16 pm

Hi.

It would be a fun little exercise to write a handler to do this, but before you do, go to the dictionary and look up the "transpose" function. You have to split your data into an array first, and then combine it back, but that is simple.

Craig Newman

EDIT. The data has to be regular, that is, cannot have differing numbers of elements in its lines. If it does, back to the cute little handler. Is this a possible issue?

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

Re: transpose a variable of data

Post by dantomlin » Fri Oct 09, 2015 8:28 pm

i'm not very experienced with arrays, so I just use variables for the most part and loop thru them...

my data is structured as it is calling data from a sql database so it always has the same number of elements in each line.

how do I split my data into an array?

Thanks, Dan

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

Re: transpose a variable of data

Post by dunbarx » Fri Oct 09, 2015 9:55 pm

Hi.

Why not do what you are comfortable with, and play with arrays later (sooner than later, though). They are powerful, but need a little practice to use effectively.

So back to adorable fun stuff. If you have some data in tab and return delimited form, make two table fields. Put a small amount of your data, perhaps just a few lines with a few elements in each line, into fld 1. This is tab and return delimited text. In a button somewhere:

Code: Select all

on mouseUp
   get fld 1
   set the itemDel to tab
   repeat with y = 1 to the number of lines of it
      repeat with u = 1 to the number of items of line y of it
         put item u of line y of it & tab after  line u of accum
      end repeat
   end repeat
   put accum into fld 2
end mouseUp
Step through this, line by line, and watch the variable "accum".

Craig

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

Re: transpose a variable of data

Post by dantomlin » Mon Oct 12, 2015 7:07 pm

thanks that worked....

Post Reply