What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Replace Problem

Replace Problem
Author: Message:
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
36 / Male / Flag
Joined: Aug 2006
O.P. Replace Problem
I'm having trouble with the following code:

js code:
// x[0] is a string
// y is an array (that in these circumstances does NOT contain x[0]
if(strip(x[0]) in y){ //strip just removes non-alphanumeric characters
    var foo = bar.replace(x[0], y[x[0]]);
    Debug.Trace(x[0] + " - " + y[x[0]]);
}

If x[0] is set to "push", "join", "reverse", "pop" or any other array method then I get "Object Expected" error and it traces:

quote:
pop -
function pop() {
    [native code]
}



Any ideas why? Everything should just be treated as a string :s I'm probably being stupid

This post was edited on 08-02-2011 at 08:23 PM by Spunky.
<Eljay> "Problems encountered: shit blew up" :zippy:
08-02-2011 08:21 PM
Profile PM Find Quote Report
matty
Scripting Guru
*****


Posts: 8336
Reputation: 109
39 / Male / Flag
Joined: Dec 2002
Status: Away
RE: Replace Problem
Couldn't you use .toString()?
08-02-2011 08:33 PM
Profile E-Mail PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
36 / Male / Flag
Joined: Aug 2006
O.P. RE: Replace Problem
Doesn't work. I get the same results
<Eljay> "Problems encountered: shit blew up" :zippy:
08-02-2011 08:36 PM
Profile PM Find Quote Report
Eljay
Elite Member
*****

Avatar
:O

Posts: 2949
Reputation: 77
– / Male / –
Joined: May 2004
RE: Replace Problem
How was y defined? for..in loops The in keyword is for Objects, not Arrays. :P

This post was edited on 08-02-2011 at 08:49 PM by Eljay.
08-02-2011 08:48 PM
Profile PM Find Quote Report
whiz
Senior Member
****


Posts: 568
Reputation: 8
– / – / Flag
Joined: Nov 2008
RE: Replace Problem
I'm assuming it's because, for objects, foo.bar is the same as  foo["bar"].  Seems calling []["foo"], where foo is an array method, always seems to do this.

Could you perhaps add a prefix or something to x as a workaround?
08-02-2011 08:48 PM
Profile E-Mail PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
36 / Male / Flag
Joined: Aug 2006
O.P. RE: Replace Problem
quote:
Originally posted by whiz
I'm assuming it's because, for objects, foo.bar is the same as  foo["bar"].  Seems calling []["foo"], where foo is an array method, always seems to do this.

Could you perhaps add a prefix or something to x as a workaround?

I'll try shortly. If that does fix it, it's a bit sloppy, but I want it working. For the purposes of the script, it doesn't really cause any errors, Just don't want all that in the debug =/

EDIT: Nope can't seem to do that without breaking the script. I need the value related to x[0] from y


quote:
Originally posted by Eljay
How was y defined? for..in loops The in keyword is for Objects, not Arrays. :P

Arrays are Objects :p

This post was edited on 08-02-2011 at 08:57 PM by Spunky.
<Eljay> "Problems encountered: shit blew up" :zippy:
08-02-2011 08:52 PM
Profile PM Find Quote Report
Eljay
Elite Member
*****

Avatar
:O

Posts: 2949
Reputation: 77
– / Male / –
Joined: May 2004
RE: Replace Problem
quote:
Originally posted by Spunky
quote:
Originally posted by Eljay
How was y defined? for..in loops The in keyword is for Objects, not Arrays. :P

Arrays are Objects :p

Well yeah, but the in keyword returns true because of all the built-in Array functions. Try using:

js code:
if(y.hasOwnProperty(strip(x[0]))

???
08-02-2011 09:00 PM
Profile PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
36 / Male / Flag
Joined: Aug 2006
O.P. RE: Replace Problem
Works perfect (Y) No dodgy try... catch statements for me :D
<Eljay> "Problems encountered: shit blew up" :zippy:
08-02-2011 09:02 PM
Profile PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
32 / Male / Flag
Joined: Apr 2004
RE: Replace Problem
How is y defined? If you're storing values in an Array variable using string keys, then you're storing them as properties in the Object and not as elements in the Array.
js code:
var a = [];
a[0] = "hello"; // stored as array element
Debug.Trace(a.length); // = 1, as you would expect
a["foo"] = "bar"; // stored as object property
Debug.Trace(a.length); // = 1, problem?

Debug.Trace(a.shift()); // = "hello", as you would expect
Debug.Trace(a.shift()); // = undefined, instead of the 'expected' "bar"
If you don't need any of the Array features such as .length, .pop() or .slice(), simply make it an Object:
js code:
var y = {};
That way, you don't need any of those .hasOwnProperty() checks to skip any unwanted methods from the prototype, since Object.prototype should be empty.

It'd also help if you gave your variables decent names. How will you know what x[0] and y were when you look at your code again in a few months?
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
08-03-2011 08:50 AM
Profile E-Mail PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On