Page 1 of 1

Creating array

Posted: Tue Jun 26, 2012 10:29 pm
by vladoportos
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

Re: Creating array

Posted: Tue Jun 26, 2012 10:55 pm
by dunbarx
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

Re: Creating array

Posted: Wed Jun 27, 2012 7:53 am
by dave_probertGA6e24
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

Re: Creating array

Posted: Wed Jun 27, 2012 10:23 am
by vladoportos
Thanks both, Daves solution is the simplest :D

Re: Creating array

Posted: Wed Jun 27, 2012 10:58 am
by snm
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

Re: Creating array

Posted: Wed Jun 27, 2012 2:06 pm
by dunbarx
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

Re: Creating array

Posted: Wed Jun 27, 2012 3:36 pm
by dave_probertGA6e24
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.
;) ;) ;)