Search the web
Sign In
New User? Sign Up
jsosa · JavaScript OSA-Talk
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Real people. Real stories. See how Yahoo! Groups impacts members worldwide.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Out of Memory; delete   Message List  
Reply | Forward Message #423 of 529 |
Re: [jsosa] Out of Memory; delete

--- Gavin Kistner <gavin@...> wrote:
> I get an Out of Memory error when I attempt to
> create the sort comparison function using new
> Function(..).

> function SortBy(){
> var args = (arguments.length==1 &&
>
arguments[0].constructor==Array)?arguments[0]:arguments;
> var body = "return ";
> for (var i=0,len=args.length;i<len;i++){
> var param = args[i];
> if (typeof(param)=='string'){
> var colName=param;
> var sortAsc=1;
> } else for (var colName in param) sortAsc =
> param[colName];
>
>
body+='a.'+colName+(sortAsc?'<':'>')+"b."+colName+"?-1:
>
> a."+colName+(sortAsc?'>':'<')+"b."+colName+"?1:";
> }
> return new Function('a','b',body+0);
> }

I don't get an Out of Memory message. I don't have
any iTune thingies, so I just tested with this:

var f = SortBy( 'a', 'b', 'c' );

var obj1 = { 'a' : 1, 'b' : 2, 'c' : 1 }
var obj2 = { 'a' : 1, 'b' : 1, 'c' : 2 }
var obj3 = { 'a' : 2, 'b' : 1, 'c' : 1 }

var arr = [ obj1, obj2, obj3 ];

arr.sort( f )


It worked fine. I also re-wrote this function in a
way that, while possibly not as fast as yours, is at
least a little more readable:


function SortBy( )
{
// Caller passes either a single array, or
multiple params
//
if ( arguments[0].constructor == Array ) arguments
= arguments[0];

if ( typeof args == 'string' ) args = arguments;

/* This is what we want it to look like:
* if ( a.name < b.name ) { return -1 }
else
* if ( a.name > b.name ) { return 1 }
else
* if ( a.artist < b.artist ) { return -1 }
else
* if ( a.artist > b.artist ) { return 1 }
else
* if ( a.album < b.album ) { return -1 }
else
* if ( a.album > b.album ) { return 1 }
else { return 0 }
*/
var src = '';

// A little trick that adds readability
//
var tmp1 = 'if ( a.PROP < b.PROP ) { return -1 }
else \n';
var tmp2 = 'if ( a.PROP > b.PROP ) { return 1 }
else \n';

for (var i = 0; i < arguments.length; i++ )
{
src += tmp1.replace( /PROP/g, arguments[i] );
src += tmp2.replace( /PROP/g, arguments[i] );
}
src += '{ return 0 }'

// return src; // DEBUGGING

return new Function( 'a', 'b', src );
}

> I want to delete the files. Since delete is a
> reserved keyword, something like itunes.delete(song)
> doesn't work. How do I use methods/Actions which are
> reserved keywords in JS? I'm trying:
>
> itunes['delete'](song)
>
> now, which at least compiles, but I'm not sure if
> that's the right call method.

Yeah, that's what you have to do. Mark addresses
this in the online documentation:

<http://www.latenightsw.com/freeware/JavaScriptOSA/MacOS.AEClass.html>

>> ... the standard Delete AppleEvent collides with
>> JavaScript's delete reserved word. ... use the
>> array syntax to define the AppleEvent you want to
>> send
>>
>> with (MacOS.filder())
>> items["File To Delete.txt"]["delete"]();

{ Arthur J. Knapp;
<mailto:arthur@...>;

What...? Oh...!
}


__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/



Thu Dec 4, 2003 5:04 pm

admiralnovia
Offline Offline
Send Email Send Email

Forward
Message #423 of 529 |
Expand Messages Author Sort by Date

I'm trying to shove all the songs in my iTunes library into an array so that I can sort them and then remove a ton of duplicates. However, using the following...
Gavin Kistner
rabyak
Online Now Send Email
Dec 1, 2003
10:29 pm

... arguments[0].constructor==Array)?arguments[0]:arguments; ... I don't get an Out of Memory message. I don't have any iTune thingies, so I just tested with...
Arthur Knapp
admiralnovia
Offline Send Email
Dec 4, 2003
5:04 pm

... Hrm. This was with Script Editor...are you using Script Debugger or something else? After I wrote this message, I ran into various other areas where ...
Gavin Kistner
rabyak
Online Now Send Email
Dec 4, 2003
5:09 pm

... thingies, ... I tried it in both, it worked fine. I don't think the issue is with your SortBy() function, but rather with the .sort() method that follows. ...
Arthur Knapp
admiralnovia
Offline Send Email
Dec 4, 2003
9:17 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help