473,511 Members | 14,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is it possible to....

Is it possible to swap two elements of an array? for example like swap
rows in the matrix:

[1 5 7 3]
[2 6 2 5]
[6 5 1 7]

where I make a matrix as an array of arrays. where the inner arrays are
the rows of the matrix. so the above could be coded as:

A = [
[1,5,7,3],
[2,6,2,6],
[6,5,1,7]
];

Jul 23 '05 #1
5 1096
greenflame wrote:
Is it possible to swap two elements of an array? for example like swap
rows in the matrix:

[1 5 7 3]
[2 6 2 5]
[6 5 1 7]

where I make a matrix as an array of arrays. where the inner arrays are
the rows of the matrix. so the above could be coded as:

A = [
[1,5,7,3],
[2,6,2,6],
[6,5,1,7]
];


Below is some commented code with alerts to show what is happening
- you can safely remove the alerts.

You may want to check that A, A[r1] and A[r2] actually exist before
trying to swap them.
function swapRows( A, r1, r2 ){
var i = A.length;
alert(A.join('\n'));
A[i] = A[r1]; // Copy r1 to an extra element
alert(A.join('\n'));
A[r1] = A[r2]; // Copy r2 into r1
alert(A.join('\n'));
A[r2] = A[i]; // Put remembered r1 into r2
alert(A.join('\n'));
delete A[i]; // Remove copy of r1
alert(A.join('\n'));
}

B = [
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
];

swapRows( B, 0, 2 );
--
Rob
Jul 23 '05 #2
RobG wrote:
[...]
function swapRows( A, r1, r2 ){
var i = A.length;
alert(A.join('\n'));
A[i] = A[r1]; // Copy r1 to an extra element
alert(A.join('\n'));
A[r1] = A[r2]; // Copy r2 into r1
alert(A.join('\n'));
A[r2] = A[i]; // Put remembered r1 into r2
alert(A.join('\n'));
delete A[i]; // Remove copy of r1
alert(A.join('\n'));


Forgot to mention that as A is just a reference to B, whatever we do
to A also happens to B, so:

alert(B.join('\n'));

shows that B now has its rows swapped.

[...]
--
Rob
Jul 23 '05 #3
Jc
greenflame wrote:
Is it possible to swap two elements of an array?


Sure, array's are read/write, so just use a temporary var while
swapping so you don't lose one of the array items.

Example:

<script>
var a1 = [1,2,3];
var a2 = [4,5,6];
var aMatrix = [a1, a2];
alert("before swap:\n"+aMatrix.join("\n"));

var aTemp = aMatrix[0];
aMatrix[0] = aMatrix[1];
aMatrix[1] = aTemp;
alert("after swap:\n"+aMatrix.join("\n"));
</script>

Jul 23 '05 #4
Lee
greenflame said:

Is it possible to swap two elements of an array? for example like swap
rows in the matrix:

[1 5 7 3]
[2 6 2 5]
[6 5 1 7]

where I make a matrix as an array of arrays. where the inner arrays are
the rows of the matrix. so the above could be coded as:

A = [
[1,5,7,3],
[2,6,2,6],
[6,5,1,7]
];


Certainly.
But I can't be more specific without more than a "for example".

Jul 23 '05 #5
I had a problem with the part where you remove the last row. It didnt
remove it, well yes but no. Anyway I fixed it by using the pop command.
Thanks!

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
14461
by: Julia Briggs | last post by:
I am struggling to create a PHP function that would take a specified image (JPG, GIF or PNG) from a link, and resize it down to a thumbnail so it will always fit in a 200x250 space. I am hoping...
36
9431
by: rbt | last post by:
Say I have a list that has 3 letters in it: I want to print all the possible 4 digit combinations of those 3 letters: 4^3 = 64 aaaa
20
2438
by: CHIN | last post by:
Hi all.. here s my problem ( maybe some of you saw me on other groups, but i cant find the solution !! ) I have to upload a file to an external site, so, i made a .vbs file , that logins to...
7
2335
by: Andrzej | last post by:
Is it possible to call a function which name is given by a string? Let assume that I created a program which call some functions for example void f1(void), void f2(void), void f3(void). ...
2
3785
by: Bhupesh Naik | last post by:
This is a query regarding my problem to make a spell and grammar check possible in text area of a web page. We have aspx pages which are used to construct letters. The browser based screens...
1
6943
by: AAA | last post by:
hi, I'll explain fastly the program that i'm doing.. the computer asks me to enter the cardinal of a set X ( called "dimX" type integer)where X is a table of one dimension and then to fill it...
25
2518
by: Piotr Nowak | last post by:
Hi, Say i have a server process which listens for some changes in database. When a change occurs i want to refresh my page in browser by notyfinig it. I do not want to refresh my page i.e....
4
7644
by: RSH | last post by:
Okay my math skills aren't waht they used to be... With that being said what Im trying to do is create a matrix that given x number of columns, and y number of possible values i want to generate...
7
3346
by: Robert S. | last post by:
Searching some time now for documents on this but still did not find anything about it: Is it possible to replace the entry screen of MS Office Access 2007 - that one presenting that default...
14
1988
by: bjorklund.emil | last post by:
Hello pythonistas. I'm a newbie to pretty much both programming and Python. I have a task that involves writing a test script for every possible combination of preference settings for a software...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7417
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.