GameMonkey Script

GameMonkey Script Forums
It is currently Thu May 23, 2019 2:59 am

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: List - is there one?
PostPosted: Sun Jul 19, 2009 7:46 pm 
Offline

Joined: Sun Jul 29, 2007 11:52 am
Posts: 33
Location: Canberra, Australia
I didn't think there was a list in GM, just noticed there was a gmArrayLib, but that didn't suit my needs. I decided I'd quickly craft one, figured I'd share it here - it seems to work fine, works much like Python's list, where it's actually just a resizable array. Currently it does not resize when items are removed, but I plan to add utility stuff to this as I go along, such as the likes of Python zip - which can be pretty handy, and may be a nifty little feature to have in GM too :) I'm also planning on crafting a tuple too.

Anyways - the code is attached, it's just gmArrayLib, but modified with more 'list-like' operations on it. Here is a block of code I've been using to test if it works, and also the output:

Code:
DumpList = function( text, thelist )
{
   i = 0;
   print( "=======\n" );
   print( text );
   while( i < thelist.Length() )
   {
      print( thelist[ i ] );
      print( ",\n" );
      i = i + 1;
   };
   print( "=======\n\n" );
};


bleh = list( 1, 2, 2, 3, "hello", "hello", 5, 5, "world" );

DumpList( "List Creation:\n", bleh );

bleh.Append( 1 );
DumpList( "Append 1 to end of list:\n", bleh );

bleh.Remove( 2 );
DumpList( "Remove all occurances of 2:\n", bleh );

bleh.RemoveAt( 1 );
DumpList( "Remove element at index 1:\n", bleh );

bleh.Remove( "hello", 5 );
DumpList( "Remove all occurances of hello and 5s:\n", bleh );


Code:
=======
 List Creation:
 1 ,
 2 ,
 2 ,
 3 ,
 hello ,
 hello ,
 5 ,
 5 ,
 world ,
 =======

 =======
 Append 1 to end of list:
 1 ,
 2 ,
 2 ,
 3 ,
 hello ,
 hello ,
 5 ,
 5 ,
 world ,
 1 ,
 =======

 =======
 Remove all occurances of 2:
 1 ,
 3 ,
 hello ,
 hello ,
 5 ,
 5 ,
 world ,
 1 ,
 =======

 =======
 Remove element at index 1:
 1 ,
 hello ,
 hello ,
 5 ,
 5 ,
 world ,
 1 ,
 =======

 =======
 Remove all occurances of hello and 5s:
 1 ,
 world ,
 1 ,
 =======


Edit: I've just realised you can treat a table like a list -_- however it doesn't have the ability to do many list operations does it?


Attachments:
gmListLib.h [2.32 KiB]
Downloaded 321 times
gmListLib.cpp [9.61 KiB]
Downloaded 329 times
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group