Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-31-2007, 11:57 AM   #1 (permalink)
markster
Code Monkey
 
markster's Avatar
 
Join Date: Jun 2006
Posts: 65
markster is on a distinguished road
Question Swap elements in array?

Is there any kind of code that will swap the elements that are in the middle of an array, eg:

Code:
var array = new Array("a","b","c","d","e","f","g","h");
swap(array,"b","d");
So it outputs a,d,c,b,e,f,g,h (b and d have swapped places).

If anyone has code that can do this or some sort of starting point for me I would be very thankful.
__________________
Stuff4Web.co.uk - The ultimate resource for webmasters

50% off all orders £1-£50! Just enter the code AGSIG01 at the checkout! Offer ends: 01/09/2008
markster is offline   Reply With Quote
Old 08-31-2007, 12:12 PM   #2 (permalink)
markster
Code Monkey
 
markster's Avatar
 
Join Date: Jun 2006
Posts: 65
markster is on a distinguished road
Actually, never mind, I've got it. I swept the cobwebs from my mind and put it to work. Heres what I've come up with:

Code:
function swap(array,index1,index2) {
	var firstRep = array[index1];
	var secondRep = array[index2];
	var output = new Array();
	for (ar in array) {
		if (ar==index1) {
			output[index1] = secondRep;
		} else if (ar==index2) {
			output[index2] = firstRep;
		} else {
			output[ar] = array[ar];
		}
	}
	return output;
}
function test() {
	var output = "";
	var start = new Array("a","b","c","d","e","f","g");
	for (st in start) {
		output += start[st];
	}
	alert(output);
	output = "";
	var newArray = swap(start,1,3);
	for (ne in newArray) {
		output += newArray[ne];
	}
	alert(output);
}
__________________
Stuff4Web.co.uk - The ultimate resource for webmasters

50% off all orders £1-£50! Just enter the code AGSIG01 at the checkout! Offer ends: 01/09/2008
markster is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with array Michael12 Java 3 05-01-2007 11:53 PM
creating 3-dimensional array nesvarbu Platform/API C++ 3 05-09-2006 07:01 AM
Sorting Help Salchester Standard C, C++ 13 10-17-2005 12:10 PM
Sorting an array of any Comparable objects plague Java 1 03-14-2005 06:24 PM
Simple reverse programm silex Standard C, C++ 3 01-22-2005 08:03 AM


All times are GMT -8. The time now is 08:09 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting