Digital Workshop

Welcome to the Digital Workshop Message Boards
It is currently December 23rd, 2024, 2:06 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: How to remove an element from an array?
PostPosted: April 8th, 2010, 9:30 am 
Offline

Joined: November 8th, 2004, 5:23 pm
Posts: 279
Opus: Opus Pro 9
OS: Win 10/64
I wonder how it is possible to remove/delete elements from an array.
Since I did not find such a command in the help files, I have already tried possible undocumented commands like splice or delete but without success.

Thanks in advance.

T.

_________________
Opus Pro 9.75, Win 10/64, 8 GB RAM, Intel(R) Core(TM) i5-3570K CPU @ 3.40GHz (4 CPUs), ~3.4GHz, NVIDIA GeForce GTX 660 Ti, Roland OctaCapture


For this message Tarantoga has been thanked by : mackavi


Top
 Profile Visit website  
 
 Post subject: Re: How to remove an element from an array?
PostPosted: April 8th, 2010, 9:56 am 
Offline
Godlike
Godlike
User avatar

Joined: March 21st, 2007, 10:44 am
Posts: 3188
Location: UK
Opus: Evolution
Sorry T,

Opus Script is ECMA v1.1 and those Array methods don't exist. You can, with a bit of magic, add your own method and there are some posts on that around here somewhere or create just use the function bit.

Remember though that arrays are passed and assigned by reference.

Mack

_________________
When you have explored all avenues of possibilities, what ever remains, how ever improbable, must be the answer.

Interactive Solutions for Business & Education
Learn Anywhere. Learn Anytime.

www.interaktiv.co.uk
+44 (0) 1395 548057


Top
 Profile Visit website  
 
 Post subject: Re: How to remove an element from an array?
PostPosted: April 8th, 2010, 1:36 pm 
Offline

Joined: November 8th, 2004, 5:23 pm
Posts: 279
Opus: Opus Pro 9
OS: Win 10/64
... ouch ... I found out that there is also no possibility to copy arrays without the slice function. Since I am not a real programmer I would be glad if someone has an idea how to find a solution for this. How do you handle problems where lists are needed? In my case it's only a little game but I have no idea how to do this without lists so far.


T.

Quote:
some posts on that around here somewhere or create just use the function bit.


... I would like to do that but as mentioned above I am no real programmer and don't really know what you mean with your suggestion. :oops:

_________________
Opus Pro 9.75, Win 10/64, 8 GB RAM, Intel(R) Core(TM) i5-3570K CPU @ 3.40GHz (4 CPUs), ~3.4GHz, NVIDIA GeForce GTX 660 Ti, Roland OctaCapture


For this message Tarantoga has been thanked by : mackavi


Top
 Profile Visit website  
 
 Post subject: Re: How to remove an element from an array?
PostPosted: April 8th, 2010, 3:52 pm 
Offline
User avatar

Joined: October 25th, 2004, 10:33 am
Posts: 257
Location: UK
Opus: Pro 8
OS: Windows 7 Professional x64
System: Dell XPS15 i7x4 2.1Ghz 6GB 128GB SSD
Something like this:

Code:
function RemoveFromArray(theArray, position)
{
   if(position < theArray.length)
   {
      while(position < theArray.length-1)
      {
         // copy element
         theArray[position] = theArray[position+1];
      }
      theArray.length--;
   }
   
}


But note how inneficient it is. Arrays aren't a good choice if you need to remove things from the middle, you're better off using a linked list. It's okay for half a dozen elements, but if you have thousands the remove is going to be slow.


For this message Dave Emberton has been thanked by : mackavi


Top
 Profile Visit website  
 
 Post subject: Re: How to remove an element from an array?
PostPosted: April 8th, 2010, 5:42 pm 
Offline

Joined: November 8th, 2004, 5:23 pm
Posts: 279
Opus: Opus Pro 9
OS: Win 10/64
Dave,

thanks a lot for your help.

Quote:
you're better off using a linked list.


Since I like to learn (and besides that have actually a lot of items in this list): Would you be so kind to explain what you mean with "linked list". I have already searched the forum but could not find any information about this topic)

I'm not looking for a ready to use solution but like to understand how this works.

Thanks in advance

T. :)

_________________
Opus Pro 9.75, Win 10/64, 8 GB RAM, Intel(R) Core(TM) i5-3570K CPU @ 3.40GHz (4 CPUs), ~3.4GHz, NVIDIA GeForce GTX 660 Ti, Roland OctaCapture


For this message Tarantoga has been thanked by : mackavi


Top
 Profile Visit website  
 
 Post subject: Re: How to remove an element from an array?
PostPosted: April 8th, 2010, 6:30 pm 
Offline
Godlike
Godlike
User avatar

Joined: March 21st, 2007, 10:44 am
Posts: 3188
Location: UK
Opus: Evolution
Tarantoga wrote:
I have already searched the forum but could not find any information about this topic


They'd be in the Advanced+1 section :-)

OpusScript doesn't have linked lists, you'd have to write your own - now there's a challenge - where's Lar?

As for what they are:

Image you have have three objects call them Dave, Mack and Tarantoga. To get from Dave to Tarantoga you have to go through Mack much in the same way as you do with Arrays. Except with linked lists the way you navigate through the list is by querying each object.

IE, you ask Dave who is before him and he replies -'nobody' - you ask him who is after him and he replies - 'Mack'

Do the same with Mack and he replies that Dave is before him and Tarantoga is after him.

Now if you want to remove Mack from this (great idea at this point) and rather than move everybody down the list (there could be every forum member after Tarantogo in this list) you simply tell Dave that Tarantoga is after him and tell Tarantoga that Dave is before him and Mack has gone!

You need to use objects, rather than variables, because each list items needs three bits of information, the value held, the object before and the object after.

Mack

_________________
When you have explored all avenues of possibilities, what ever remains, how ever improbable, must be the answer.

Interactive Solutions for Business & Education
Learn Anywhere. Learn Anytime.

www.interaktiv.co.uk
+44 (0) 1395 548057


Top
 Profile Visit website  
 
 Post subject: Re: How to remove an element from an array?
PostPosted: April 8th, 2010, 7:54 pm 
Offline
Godlike
Godlike

Joined: November 12th, 2005, 1:56 am
Posts: 1474
Location: SFBay Area
Opus: OpusPro v9.0x, & Evol.
OS: Vista32
System: Core 2 duo 2Ghz, RAM 3GB, Nvidia Go 7700 - laptop
Tar,

Can you give some idea of the scope here?
How large is the array? Array or arrays?
How do you identify which array element to delete? By content or by index?

I have an idea for creating a 'mask'... may serve your purpose to filter out elements. Then again, whether this is practical and efficient depends on your next steps and methods after getting the modified array.

_________________
_good things come to those who wait(0)_


For this message Lar_123 has been thanked by : mackavi


Top
 Profile  
 
 Post subject: Re: How to remove an element from an array?
PostPosted: April 8th, 2010, 9:26 pm 
Offline

Joined: November 11th, 2004, 4:05 am
Posts: 636
Location: Christchurch, NZ
What's wrong with using the built in "delete"?

/*******************************************/
arr = new Array("A","B","C");
delete arr[0];
// removes "A" from the array...
Debug.trace(arr);
/*******************************************/

Paul


For this message Paul has been thanked by : mackavi


Top
 Profile  
 
 Post subject: Re: How to remove an element from an array?
PostPosted: April 8th, 2010, 10:08 pm 
Offline
Godlike
Godlike
User avatar

Joined: March 21st, 2007, 10:44 am
Posts: 3188
Location: UK
Opus: Evolution
Surely it hasn't actually gone - just become undefined?

Code:
Debug.trace(arr.length)


Mack

_________________
When you have explored all avenues of possibilities, what ever remains, how ever improbable, must be the answer.

Interactive Solutions for Business & Education
Learn Anywhere. Learn Anytime.

www.interaktiv.co.uk
+44 (0) 1395 548057


Top
 Profile Visit website  
 
 Post subject: Re: How to remove an element from an array?
PostPosted: April 8th, 2010, 10:17 pm 
Offline

Joined: November 11th, 2004, 4:05 am
Posts: 636
Location: Christchurch, NZ
Hi Mack,
Yes... Just checked my notes... :shock:
Definitely "Undefined" using the script below:

/*****************************************/
arr = new Array("A","B","C");
delete arr[0];

var i;
display="";
for (var i = 0; i <= arr.length-1; i++)
display = display + (arr[i] + "\n" );
Debug.trace(display)
/*****************************************/


So I'd probably use something like:

/**********************************************************/

// define method
function arrayRemove(n){
if (!n) return;
var i, l = this.length;
for (i=n+1; i < l; i++)
this[i-1] = this[i];
this.length--;
}

if (typeof(Array.prototype.remove) == "undefined") {
Array.prototype.remove = arrayRemove;
}

// usage
// create array
var a1 = new Array("a","b","c","d");

Debug.trace("Initial array is "+a1+"\n")
Debug.trace("remove third element 'c' from array a1 with a1.remove(2)"+"\n")
a1.remove(2);
Debug.trace("new array is "+a1)

/******************************************************/


Paul


For this message Paul has been thanked by : mackavi


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 7 guests


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 © 2000, 2002, 2005, 2007 phpBB Group