Creating array

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
vladoportos
Posts: 17
Joined: Wed Jun 20, 2012 8:21 pm

Creating array

Post by vladoportos » Tue Jun 26, 2012 10:29 pm

Hello all,
I know this is a "noob" question but I'm kind of stack on it.

I have small application that take all files from given folder and put it to DB ( SQLite ) and than display in Datagrid.

What I'm trying to do is to have the type of file separated like exe, zip, jpg etc... but rom the list I can't just set delimiter to "." and take the last item as there can be dot in name of file, or it don't have .<something> at all so it doesn't work all the time.

I thought that I create array of most used file extensions eg:

Code: Select all

put "exe,rar,html,htm,bin,zip,pdf,mp3,jpg,wmv,avi,tif,ppt,mov,doc,docx,mpg,psd,sitx,sit,cdr,ai,xls,mp4,txt,m4a,rmvb,gif,swf,png,dat,chm,jar,iso,flv,cda,7z,fla,jpeg,3gp,aiff,cdl,vob,xtm,ace,m4p,pst,ttf,bin,ifo,tgz,log,dll,ram,torrent,ses,vcd,bat,sql,cbr,pub" into vExt
and than create array from that and later in code I search if the last item of file name match any of this and if yes than it should write it DB in separate row...

I'm stuck on how to create array automatically from the long variable..

I know it should probably look like:

Code: Select all

   set itemDel to ","
   repeat for each item in vExt
      <something to create array>
   end repeat 
I'm quite new to LiveCode so have patience :)

Regards,
Vladimir

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

Re: Creating array

Post by dunbarx » Tue Jun 26, 2012 10:55 pm

Hi.

Try this in a button script:

Code: Select all

on mouseUp
   put "exe,rar,html,htm,bin,zip,pdf,mp3,jpg,wmv,avi,tif,ppt,mov,doc,docx,mpg,psd,sitx,sit,cdr,ai,xls,mp4,txt,m4a,rmvb,gif,swf,png,dat,chm,jar,iso,flv,cda,7z,fla,jpeg,3gp,aiff,cdl,vob,xtm,ace,m4p,pst,ttf,bin,ifo,tgz,log,dll,ram,torrent,ses,vcd,bat,sql,cbr,pub" into vExt
   
   repeat with y = 1 to the number of items of vExt
      put item y of vExt into yourArray[y}
   end repeat
   
   set itemdel to "-"
   combine yourArray with return and "-"
   sort yourArray numeric by item 1 of each
   put yourArray into  fld "yourField"
   
end mouseUp
You will need a field "yourField" to see the results. Note that the array you are building is complete after the repeat loop ends. The rest of the handler deconstructs it so it can be displayed in a field.

Craig Newman

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Re: Creating array

Post by dave_probertGA6e24 » Wed Jun 27, 2012 7:53 am

Or you could simply use:

Code: Select all

split vExt by comma
This will convert vExt from a string to an array - splitting each part by a comma.

It's in the dictionary :)

Hope that helps.
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

vladoportos
Posts: 17
Joined: Wed Jun 20, 2012 8:21 pm

Re: Creating array

Post by vladoportos » Wed Jun 27, 2012 10:23 am

Thanks both, Daves solution is the simplest :D

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: Creating array

Post by snm » Wed Jun 27, 2012 10:58 am

Drag DataGrid and button onto your stack.
Add needed columns into DataGrid in Inspector > Columns pane, and name them (ex.: "Nr" and "ext")
Put script to button:

Code: Select all

on mouseUp
   local vExt,yourArray
   
   put "exe,rar,html,htm,bin,zip,pdf,mp3,jpg,wmv,avi,tif,ppt,mov,doc,docx,mpg,psd,sitx,sit,cdr,ai,xls,mp4,txt,m4a,rmvb,gif,swf,png,dat,chm,jar,iso,flv,cda,7z,fla,jpeg,3gp,aiff,cdl,vob,xtm,ace,m4p,pst,ttf,bin,ifo,tgz,log,dll,ram,torrent,ses,vcd,bat,sql,cbr,pub" into vExt
      
   repeat with y = 1 to the number of items of vExt
      put y into yourArray[y]["Nr"]
      put item y of vExt into yourArray[y]["ext"]
      --put anythingElse into yourArray[y][anythingElse]
   end repeat
   
   set the dgData of grp "DataGrid" to yourArray
      
end mouseUp
Then click the button.
Is it what you asked about?

Marek

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

Re: Creating array

Post by dunbarx » Wed Jun 27, 2012 2:06 pm

Dave's offering is certainly the most compact.

But I think you should do it the long way until you are more comfortable with arrays. For example, Dave's method, using only a single delimiter, automatically creates an array with numeric keys. Well, so did mine, but I did so by explicitly assigning numeric keys in each pass of a repeat loop. There are times, more often than not, actually, when you want arrays built with keys of your own design.

Names associated with addresses, for example.

Anyway, the right way to learn this is to build arrays. Can you write a script that makes an array with two delimiters, with non numeric keys? How about that address book thingie?

Craig Newman

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Re: Creating array

Post by dave_probertGA6e24 » Wed Jun 27, 2012 3:36 pm

I have to agree with Craig about doing things the long way until you are happy with the language - it does make for better learning.

Dave

PS, you can also do:

Code: Select all

split vString by comma and tab 

to do a two dimension break of a string into an array.
;) ;) ;)
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

Post Reply