Shoutbox

HELP - "foreach" command! - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: HELP - "foreach" command! (/showthread.php?tid=90737)

HELP - "foreach" command! by whiz on 05-21-2009 at 06:36 PM

Is there a way of doing something for every item in an array?  I thought there was something called "foreach" that does this...  then again, I don't really know.

For example:

Javascript code:
var Test = new Array("test1", "test2", "test3");
var TestDesc = new Array("This is Test 1.", "This is Test 2.", "This is Test 3.");
 
var TestWnd = MsgPlus.CreateWnd("Windows.xml", "WndTest", 0);
 
foreach (Test)
{
    TestWnd.LstView_AddItem("LstTest", Test);
    TestWnd.LstView_SetItemText("LstTest", 0, 1, TestDesc)
}


RE: HELP - "foreach" command! by SmokingCookie on 05-21-2009 at 07:35 PM

You mean something like:

JScript code:
for(var i = 0; i < MyArray.length; i++) {
    <code_goes_here>
}


I may recommend using a different solution than using 2 arrays and hoping they will remain synchronised:

JScript code:
/* CLASS Item([string] ID: the ID you stored in the Test array,
[string] Desc: the description you stored in the TestDesc array);*/

var Item = function(ID,Desc) {
    return {
        "ID":    ID,
        "Desc":    Desc
}
 
// Array of Item instances.
var MyArray = [
    new Item(
        "test 1",
        "This is test 1"),
    new Item(
        "test 2",
        "This is test 2"),
    new Item(
        "test N",
        "This is test N")
]
 
// Assume window is loaded and is assigned to WndTest;
for(var i = 0; i < MyArray.length; i++) {
    with(WndTest) {
        LstView_AddItem("ListViewID",MyArray[i].ID);
        LstView_SetItemText("ListViewID",i,1,MyArray[i].Desc);
    }
}


Note that this is untested code.
RE: HELP - "foreach" command! by Spunky on 05-21-2009 at 07:38 PM

Javascript code:
for (var s in test){
    Debug.Trace(test[s]);
}
 


EDIT: foreach is used in PHP
RE: HELP - "foreach" command! by SmokingCookie on 05-21-2009 at 07:41 PM

[offtopic] LOL 3-minute intervals :P[/offtopic]

for...in is also a solution, but more used for Objects.

EDIT::

Looking at Whiz' code again, adding an Array to a list-view won;t work too well.


RE: HELP - "foreach" command! by whiz on 05-21-2009 at 08:01 PM

Umm...  I can't seem to get that to work - I just get an error saying "Expected }".

Basically, I have a main (editor) window, like this:

Javascript code:
var WndWriterEditor = MsgPlus.CreateWnd("Windows.xml", "WndWriterEditor", 0);

Let's say, the user creates a new item with the AddItem window:
Javascript code:
var WndWriterAddItem = MsgPlus.CreateWnd("Windows.xml", "WndWriterAddItem", 0);

The form includes an ID, title, description.  The user enters the following information:
ID = "Test", Title = "My Little Test", and Description = "This is a little test of mine...".
Javascript code:
var WndID = WndWriterAddItem.GetControlText("EdtID");
var WndTitle = WndWriterAddItem.GetControlText("EdtTitle");
var WndDesc = WndWriterAddItem.GetControlText("EdtDesc");

I need that to be added to a ListViewControl, like:
Javascript code:
WndWriterEditor.LstView_AddItem("LstWindows", WndID);
WndWriterEditor.LstView_SetItemText("LstWindows", 0, 1, WndTitle);
WndWriterEditor.LstView_SetItemText("LstWindows", 0, 2, WndDesc);

But it needs to work out how many items have been made, so it can add data to the right ones, and it needs to be done with every filled-in form.  I also need it so that they can be edited and deleted.  I know that you can check if a particular ListViewControl entry is selected:
Javascript code:
WndWriterEditor.LstView_GetSelectedState("LstWindows", 0);

But can you get it so it checks the entire list, and returns the identifier of the selected item, rather than true or false for one entry?
RE: RE: HELP - "foreach" command! by andrewdodd13 on 05-21-2009 at 08:01 PM

quote:
Originally posted by Spunky
Javascript code:
for (var s in test){
    Debug.Trace(test[s]);
}
 


EDIT: foreach is used in PHP
And several other languages, it's just not called foreach. :) [Java has for(Item : Collection), etc.]

JScript code:
for (var enumerator = new Enumerator(myCollection) ; !enumerator.atEnd(); enumerator.moveNext()) {
    // code
}


quote:
for...in is also a solution, but more used for Objects.
This does not work in JScript. In JScript for ... in is used to enumerate the *properties* of an object, whereas in VBScript it is used to enumerate members of a collection. :) Ref
RE: HELP - "foreach" command! by whiz on 05-21-2009 at 08:17 PM

Actually, I think I get that bit now.

But I still have 2 questions:

1) I know that you can check if a particular ListViewControl entry is selected, using "LstView_GetSelectedState()", but can you get it so it checks the entire list, and returns the identifier of the selected item, rather than true or false for one entry?  Or would that have to use the "for (s in test)" thing as well?

2) How can you add another item to an array, without having to use "variable[n]"?  Can you do something like "variable += 'value'"?


RE: HELP - "foreach" command! by SmokingCookie on 05-22-2009 at 07:11 AM

Question 2: myArray.push(ItemVar); whereas ItemVar can be anything (even an array)

Question 1: No, there is not a method to retrieve the current selection. You can, however, do this:

JScript code:
var SelectedItem = -1;
 
function OnWndTestEvent_LstViewClicked(PlusWnd,ControlId,ItemIdx) {
    if(ControlId == "vLstTest") {
        SelectedItem = ItemIdx;
        <do_other_stuff>
    }
}


Make sure though that you catch all selection-related events and set SelectedItem accordingly. They are:

  • OnWindowIdEvent_LstViewClicked();
  • OnWindowIdEvent_LstViewDblClicked();
  • OnWindowIdEvent_LstViewRightClicked();

A value of -1 means that no item is currently selected. That means, for example, there is only one item in the list-view control, and you click below that item.

Also, make sure to set SelectedItem back to -1 when the window is destroyed.
RE: HELP - "foreach" command! by whiz on 05-22-2009 at 12:49 PM

Javascript code:
var WndID = new Array();
var WndTitle = new Array();
var WndDesc = new Array();
 
function OnWndWriterAddItemEvent_CtrlClicked(objWnd, strControlId)
{
    switch (strControlId)
    {
        case "BtnAdd":
            WndID.push(objWnd.GetControlText("EdtID");
            WndTitle.push(objWnd.GetControlText("EdtTitle");
            WndDesc.push(objWnd.GetControlText("EdtDesc");
            break;
    }
}

Will that work?
RE: HELP - "foreach" command! by SmokingCookie on 05-22-2009 at 12:54 PM

Short answer: yes.


RE: HELP - "foreach" command! by whiz on 05-22-2009 at 04:17 PM

So, if ".push" adds an item (I'm assuming to the end of the list), how can you remove an item from an array?

I just looked on a JavaScript site, and it says that ".splice" can be used.  Does this work in Messenger Plus! Live?


RE: HELP - "foreach" command! by SmokingCookie on 05-22-2009 at 04:20 PM

Yep. MyArray.splice(index) is what you'll need to do.


RE: HELP - "foreach" command! by whiz on 05-22-2009 at 05:07 PM

Thanks!  :D


RE: HELP - "foreach" command! by matty on 05-22-2009 at 05:12 PM

Google... http://www.w3schools.com/jsref/jsref_obj_array.asp


RE: HELP - "foreach" command! by felipEx on 05-22-2009 at 05:15 PM

quote:
Originally posted by SmokingCookie
Question 1: No, there is not a method to retrieve the current selection. You can, however, do this:

JScript code:
var SelectedItem = -1;
 
function OnWndTestEvent_LstViewClicked(PlusWnd,ControlId,ItemIdx) {
    if(ControlId == "vLstTest") {
        SelectedItem = ItemIdx;
        <do_other_stuff>
    }
}


Make sure though that you catch all selection-related events and set SelectedItem accordingly. They are:

  • OnWindowIdEvent_LstViewClicked();
  • OnWindowIdEvent_LstViewDblClicked();
  • OnWindowIdEvent_LstViewRightClicked();

A value of -1 means that no item is currently selected. That means, for example, there is only one item in the list-view control, and you click below that item.

Also, make sure to set SelectedItem back to -1 when the window is destroyed.

There's another way to get it working without dealing with a variable in those events :D

JScript code:
var LVM_FIRST   = 0x1000;
var LVM_GETNEXTITEM = LVM_FIRST + 12;
var LVNI_SELECTED = 0x2;
 
var SelectedItem = PlusWnd.SendControlMessage('vLstTest', LVM_GETNEXTITEM, -1, LVNI_SELECTED);


RE: HELP - "foreach" command! by whiz on 05-22-2009 at 07:06 PM

Great!  :P  Thanks, everyone!  ;)