Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Array puzzle

Reply
Thread Tools

Array puzzle

 
 
Lasse Edsvik
Guest
Posts: n/a
 
      12-03-2004
Hello

I was wondering if you guys could help me solve this one:


I have an array that looks like this:


a(1) = "A"
a(2) = "B"
a(3) = "C"
a(4) = "D"
a(5) = "E"

And i would like to "switch" positions, i.e move A to a(4) like this:

a(1) = "B"
a(2) = "C"
a(3) = "D"
a(4) = "A"
a(5) = "E"

is that possible to do? moving values around?

/Lasse


 
Reply With Quote
 
 
 
 
Gérard Leclercq
Guest
Posts: n/a
 
      12-03-2004

tmp=a(4)
a(4)=a(1)
a(1)=tmp


 
Reply With Quote
 
 
 
 
Gérard Leclercq
Guest
Posts: n/a
 
      12-03-2004


if you want to pick a number randomly see RANDOMIZE

lets say the nr = 4

tmp=a(1)

For x = 1 to nr-1
a(x)=a(x+1)
Next

a(nr)=tmp

or something like that NOT TESTED


 
Reply With Quote
 
Ray Costanzo [MVP]
Guest
Posts: n/a
 
      12-03-2004
You'd have to create a copy of the array and then rewrite the values as you
like. I don't really see a pattern to how you want the values changed, so
if they're just arbitrary changes, code could look like this:

(Note that arrays or 0-based in VB Script)


Dim a()
ReDim a(4)
Dim b ''variant, and no ()
a(0) = "A"
a(1) = "B"
a(2) = "C"
a(3) = "D"
a(4) = "E"

b = a
b(0) = a(1)
b(1) = a(2)
b(2) = a(3)
b(3) = a(0)
b(4) = a(4)

a = b

For i = 0 To UBound(a)
Response.Write a(i) & "<br>"
Next

Ray at work

"Lasse Edsvik" <> wrote in message
news:%...
> Hello
>
> I was wondering if you guys could help me solve this one:
>
>
> I have an array that looks like this:
>
>
> a(1) = "A"
> a(2) = "B"
> a(3) = "C"
> a(4) = "D"
> a(5) = "E"
>
> And i would like to "switch" positions, i.e move A to a(4) like this:
>
> a(1) = "B"
> a(2) = "C"
> a(3) = "D"
> a(4) = "A"
> a(5) = "E"
>
> is that possible to do? moving values around?
>
> /Lasse
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
array.each puzzle from new ruby-er Gani Ruthellen Ruby 6 08-07-2007 08:11 PM
array Puzzle puzzlecracker C++ 6 01-21-2005 01:55 AM
Array puzzle Lasse Edsvik ASP General 0 12-03-2004 02:49 PM
A puzzle to puzzle you sk A+ Certification 1 07-17-2004 05:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57