Results 1 to 9 of 9

Thread: Shifting an array

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Shifting an array

    How would I shift the elements of an array to the right. For example, I want five shifts of the array to look like this:

    0 0 0 0 0
    0 0 0 0 5
    0 0 0 5 2
    0 0 5 2 1
    0 5 2 1 8
    5 2 1 8 7

    Hope this makes sense. This shift will happen once per second in a thread that will update data to graph.

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Shifting an array

    VB Code:
    1. Dim arr() As String = {"0", "0", "0", "0", "0"}
    2.         For i As Int32 = 1 To 5
    3.             For ii As Int32 = 0 To arr.Length - 2
    4.                 arr(ii) = arr(ii + 1)
    5.             Next
    6.             arr(arr.Length - 1) = i.ToString
    7.         Next

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Shifting an array

    VB Code:
    1. For i As Integer = myArray.GetUpperBound(0) To 1 Step -1
    2.     myArray(i) = myArray(i - 1)
    3. Next i
    4.  
    5. myArray(0) = 0
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: Shifting an array

    Thanks guys
    I knew it would be simple. I'm in a bit a of rush because my last day at my current job is Friday and I don't want to leave them hanging too much.

  5. #5
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Shifting an array

    If you hadn't already guessed, JMC's version shifts to the right.
    I don't live here any more.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: Shifting an array

    If you hadn't already guessed, JMC's version shifts to the right.
    Yeah, I figured that out. You would think that for what I paid for his advice, it would have been correct. Shifted things around and all is well. Thanks for the help.

  7. #7
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Shifting an array

    He asked for right shift, but the example looked like left shift.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Shifting an array

    Should look at these things longer shouldn't I? I assumed that you were shifting elements out, but it would make more sense to be shifting them in. It also seemed a bit odd that you had the iterations upside down but I didn't give it too much thought (not much to spare you understand). You probably already have it but I'd do it like this:
    VB Code:
    1. For i As Integer = 0 To myArray.Length - 2 Step 1
    2.     myArray(i) = myArray(i + 1)
    3. Next i
    4.  
    5. myArray(myArray.GetUpperBound(0)) = newValue
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: Shifting an array

    He asked for right shift, but the example looked like left shift.
    I'm an engineer so I occasionally get my left and right confused. Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width