Array puzzle

Discussion in 'ASP General' started by Lasse Edsvik, Dec 3, 2004.

  1. Lasse Edsvik

    Lasse Edsvik Guest

    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
     
    Lasse Edsvik, Dec 3, 2004
    #1
    1. Advertising

  2. tmp=a(4)
    a(4)=a(1)
    a(1)=tmp
     
    Gérard Leclercq, Dec 3, 2004
    #2
    1. Advertising

  3. 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
     
    Gérard Leclercq, Dec 3, 2004
    #3
  4. 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
    >
    >
     
    Ray Costanzo [MVP], Dec 3, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Earl Teigrob
    Replies:
    3
    Views:
    6,652
    Nedu N
    Aug 6, 2003
  2. puzzlecracker

    array Puzzle

    puzzlecracker, Jan 20, 2005, in forum: C++
    Replies:
    6
    Views:
    550
    David Hilsee
    Jan 21, 2005
  3. Lasse Edsvik

    Array puzzle

    Lasse Edsvik, Dec 3, 2004, in forum: ASP General
    Replies:
    0
    Views:
    109
    Lasse Edsvik
    Dec 3, 2004
  4. Gani Ruthellen

    array.each puzzle from new ruby-er

    Gani Ruthellen, Aug 7, 2007, in forum: Ruby
    Replies:
    6
    Views:
    138
  5. Tom P

    numpy masked array puzzle

    Tom P, Nov 17, 2013, in forum: Python
    Replies:
    1
    Views:
    71
    Joel Goldstick
    Nov 17, 2013
Loading...

Share This Page