Monday, March 22, 2010

parsing a textfile?

Is this at all possible using an AIR application?

open text file

read text file

sort into organized information

and then upload to mysql database on a website

Im new and all, but from what I gather the real power behind flex and air is the ability to use actionscript. the only programming experience i have is with PHP. I've had a hard time finding tutorials on actionscript for non-flash implementation, so im just wondering if this is even possible. i assume i'd have to use regex to sort the text file line by line into various arraycollections right?

i'm not looking for handouts on code, just a step in the right direction - even links to where i can read about what i need to be doing.

many thanks!

parsing a textfile?

The simple answer is ''Yes, it is possible.'' My background has been mainly in Perl/PHP, but I had no problem getting used to ActionScript in Flex. The help files included with the product are pretty good and there is a fair amount of online documentation. I get the sense that ActionScript is more Java like, but if you're comfortable with the *nix type languages(C, Perl, etc.) the syntax is pretty close and you won't feel out of place.

On the other hand, I see Flex as more of a front end type product, whereas I now use PHP and Perl for complicated backend functions. It's pretty much up to you. I've done a couple of AIR apps where all the logic and computation was done in AS and I could not complain about the perfomance of the end product. The size of the files that were being accessed and processed were not small, so I was a little surprized.

Good luck.

parsing a textfile?

hey, thanks for the reply.


actionscript syntax isnt too hard to get accustomed with as you mentioned. the only thing which may take a bit of time is finding built in functions for which i've found the language reference very handy (http://livedocs.adobe.com/flex/3/langref/)

here's what i have so far:

%26lt;?xml version=''1.0'' encoding=''utf-8''?%26gt;
%26lt;mx:WindowedApplication xmlns:mx=''http://www.adobe.com/2006/mxml'' layout=''absolute''
聽聽聽 width=''700'' height=''700'' borderColor=''#0F273B'' backgroundGradientAlphas=''[1.0, 1.0]'' backgroundColor=''#333333''%26gt;
聽聽聽
聽聽聽 %26lt;mx:Script%26gt;
聽聽聽聽聽聽聽 %26lt;![CDATA[
聽聽聽聽聽聽聽 import mx.collections.ArrayCollection;
聽聽聽聽聽聽聽 import flash.filesystem.*;
聽聽聽聽聽聽聽 import flash.events.*;
聽聽聽聽聽聽聽 import mx.controls.*;

聽聽聽聽聽聽聽 private var fileOpened:File = File.desktopDirectory;
聽聽聽聽聽聽聽 private var fileContents:String;
聽聽聽聽聽聽聽 private var stream:FileStream;
聽聽聽聽聽聽聽 private var array:ArrayCollection = new ArrayCollection();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 private function selectFile(root:File):void {
聽聽聽聽聽聽聽聽聽聽聽 var filter:FileFilter = new FileFilter(''Text'', ''*.txt'');
聽聽聽聽聽聽聽聽聽聽聽 root.browseForOpen(''Open'', [filter]);
聽聽聽聽聽聽聽聽聽聽聽 root.addEventListener(Event.SELECT, fileSelected);
聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 private function fileSelected(e:Event):void {
聽聽聽聽聽聽聽聽聽聽聽 var path:String = fileOpened.nativePath;
聽聽聽聽聽聽聽聽聽聽聽 filePath.text = path;
聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽 stream = new FileStream();
聽聽聽聽聽聽聽聽聽聽聽 stream.addEventListener(ProgressEvent.PROGRESS, fileProgress);
聽聽聽聽聽聽聽聽聽聽聽 stream.addEventListener(Event.COMPLETE, fileComplete);
聽聽聽聽聽聽聽聽聽聽聽 stream.openAsync(fileOpened, FileMode.READ);
聽聽聽聽聽聽聽 }

聽聽聽聽聽聽聽 private function fileProgress(p_evt:ProgressEvent):void {
聽聽聽聽聽聽聽聽聽聽聽 fileContents += stream.readMultiByte(stream.bytesAvailable, File.systemCharset);
聽聽聽聽聽聽聽聽聽聽聽 readProgress.text = ((p_evt.bytesLoaded/1048576).toFixed(2)) + ''MB out of '' + ((p_evt.bytesTotal/1048576).toFixed(2)) + ''MB read'';
聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 private function fileComplete(p_evt:Event):void {
聽聽聽聽聽聽聽聽聽聽聽 stream.close();
聽聽聽聽聽聽聽聽聽聽聽 //fileText.text = fileContents;
聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 private function process(c:String):void {
聽聽聽聽聽聽聽聽聽聽聽 if(!c.length %26gt; 0) {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Alert.show(''File contents empty!'', ''Error'');
聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽 /*
聽聽聽聽聽聽聽聽聽聽聽 array += c.split(/\n/);
聽聽聽聽聽聽聽聽聽聽聽 for(z = 0; z %26lt;= array.length; z++) {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 fileText.text = array[z];
聽聽聽聽聽聽聽聽聽聽聽 }*/
聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 ]]%26gt;
聽聽聽 %26lt;/mx:Script%26gt;
聽聽聽
聽聽聽 %26lt;mx:Text x=''10'' y=''10'' id=''filePath'' text=''Select a file...'' width=''678'' height=''22'' color=''#FFFFFF''聽 fontWeight=''bold''/%26gt;
聽聽聽 %26lt;mx:Button x=''10'' y=''40'' label=''Browse'' click=''selectFile(fileOpened)'' color=''#FFFFFF'' fontWeight=''bold'' fillAlphas=''[1.0, 1.0]'' fillColors=''[#E2E2E2, #484848]''/%26gt;
聽聽聽 %26lt;mx:Button x=''86'' y=''40'' label=''Process'' click=''process(fileContents)'' color=''#FFFFFF'' fontWeight=''bold''聽 fillAlphas=''[1.0, 1.0]'' fillColors=''[#E2E2E2, #484848]''/%26gt;
聽聽聽 %26lt;mx:TextArea x=''10'' y=''70'' id=''fileText'' width=''678'' height=''333'' editable=''false''/%26gt;
聽聽聽 %26lt;mx:Label x=''10'' y=''411'' id=''readProgress'' text='''' width=''678'' height=''19'' color=''#FFFFFF''/%26gt;
聽聽聽
%26lt;/mx:WindowedApplication%26gt;

the stuff i commented out was freezing the application (outputting file contents into a text area). the file is about 100mb but you mentioned you used large files as well so maybe thats not the problem.

getting there slowly!

No comments:

Post a Comment