Array, change the position of it's elements (1 Viewer)

Never Hide

Registered User.
Local time
Today, 07:18
Joined
Dec 22, 2011
Messages
96
Hello all,

I have a 2D array, 1st column holds dates and the 2nd one holds ones and zeros. The 1st column is the duration of the stay of someone in a hotel and the 2nd column determines if at the given date that person's room need to be cleaned(1) or not(0). Now, if the day that the room is supposed to be cleaned happens to be Sunday, then I move the "1" from that row, to the next row.
A little visual might help a bit here(for simplicity's sake I just used M-Monday,T-Tuesday etc)
Code:
[B]The default           After I make the changes[/B]
Date  |Cleaning ----> Date  |Cleaning
M     |0              M     |0 
T     |1              T     |1
W     |0              W     |0
T     |0              T     |0
F     |0              F     |0
S     |0              S     |0
[COLOR="Red"]S     |1              S     |0[/COLOR]
[COLOR="red"]M     |0              M     |1[/COLOR]
T     |1              T     |1
W     |0              W     |0

Now what I would like to do is instead of just changing the Sunday to 0 and Monday to 1, I'd like to push the array's elements from Sunday till the end, to the next position.
So something like this

Code:
[B]The default           After I make the changes[/B]
Date  |Cleaning ----> Date  |Cleaning
M     |0              M     |0 
T     |1              T     |1
W     |0              W     |0
T     |0              T     |0
F     |0              F     |0
S     |0              S     |0
[COLOR="Red"]S     |1              S     |0[/COLOR]
[COLOR="red"]M     |0              M     |1[/COLOR]
T     |1              T     |0
W     |0              W     |1

Please let me know if this isn't clear enough so I'll try to explain it better :eek:
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 14:18
Joined
Jan 20, 2009
Messages
12,860
You may be better holding your values in a recordset. This provides a Sort facility.

How are you populating the array?
 

Never Hide

Registered User.
Local time
Today, 07:18
Joined
Dec 22, 2011
Messages
96
Hi Galaxiom,
well I chose to go with the array because it's small,fast and easy, and I just use it to populate a temp table. I'm not sure that I'll be able to explain completely and in an understandable way, at least in english :( , how I'm using this array, but trust me it's the simplest way to go about what I want to do :)
So lets just say that I just have a 2D array and that in the 2nd column i have binary data(000101100).I just want to "shift"-if you like- the position of the items in the 2nd column and move them to the next position. But not all of the item of the array, from a certaint point and after that...so

Code:
arrTest(0,1)=0   ------>  arrTest(0,1)=0
arrTest(1,1)=0            arrTest(1,1)=0
arrTest(2,1)=0            arrTest(2,1)=0
arrTest(3,1)=0            arrTest(3,1)=0
arrTest(4,1)=1            arrTest(4,1)=1
[COLOR="Red"]arrTest(5,1)=1            arrTest(5,1)=0[/COLOR]
arrTest(6,1)=1            arrTest(6,1)=1
arrTest(7,1)=0            arrTest(7,1)=1
arrTest(8,1)=0            arrTest(8,1)=0
arrTest(9,1)=1            arrTest(9,1)=0
arrTest(10,1)=0           arrTest(10,1)=1

notice that I start the shifting at the arrTest(5,1) and that I also change that one to 0
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 14:18
Joined
Jan 20, 2009
Messages
12,860
No "shift" solutions native to arrays unfortunately though I think I have seen such a thing done. Allen Browne (maybe) but his function didn't allow a starting point to be designated.

It is straigtforward enough though. Use a loop to work through the array indexes from the point you want to start. Save the value to a variable and change it in the array. Then move to the next one, save that to another variable and copy the value from the first variable. You only need the two variables to hold the values.

I would create a function to do the job. In fact I would consider creating a custom class to hold the array and include a Shift Method along with a variety of other properties and methods to handle the other things you no doubt want to do with the information.
 

Never Hide

Registered User.
Local time
Today, 07:18
Joined
Dec 22, 2011
Messages
96
Thank you for your reply Galaxiom,
could you plz give me a small example? For some reason I can't wrap my head around it to make it work properly:eek:This is what I've tried
Code:
For tmpCounter = x To (arrRows - 1)
  tmpVar = searchArr(tmpCounter, 1)
  tmpVar2 = searchArr((tmpCounter + 1), 1)
  If Weekday(searchArr(tmpCounter, 0), vbMonday) = 7 Then
    searchArr(tmpCounter, 1) = 0
    searchArr((tmpCounter + 1), 1) = tmpVar
  Else
    searchArr((tmpCounter + 1), 1) = tmpVar
  End If
Next tmpCounter

Which of course is giving me the same value in all array elements after the "shifting" has started.
I'm not sure how/where to use the tmpVar2 :eek: :eek:

P.S. I use this loop inside another loop, that's why you see the "tmpCounter=x", X is the counter for the "mother loop":p if you like and it's the starting point of the "shifting"
 

Never Hide

Registered User.
Local time
Today, 07:18
Joined
Dec 22, 2011
Messages
96
Anyone out there that could give me an example on a simple 2D array?
You have an array and you want to move the position of the items(but not from the start of the array-but i don't think this is a problem) on one of the columns to the next position, and of course the last item will be gone. Maybe a Redim Preserve could be used?

Anyone out there? :(
 

mdlueck

Sr. Application Developer
Local time
Today, 00:18
Joined
Jun 23, 2011
Messages
2,631
Now what I would like to do is instead of just changing the Sunday to 0 and Monday to 1, I'd like to push the array's elements from Sunday till the end, to the next position.

You can do that sort of thing with a VBA Collection object. Specifically using the Before/After args to the Add method of the Collection class.

More about VBA Collection Class
http://www.access-programmers.co.uk/forums/showthread.php?p=1197077

And here is where I based my VBA Collection classes on:

Using Custom Collections in Microsoft Access
http://www.databaseadvisors.com/new...ng custom collections in microsoft access.asp

I cleaned up the Variant usage in the example and defined the variables as the types they actually are... such as the Key is a string.

And like I said at the end of the discussion thread I first linked to... I ended up leaving the collection unsorted, did a sorted table scan of the possible entries, and did an "If Exist" against the Collection. That ended up being more efficient than having to keep sorting the Collection as elements were added to it. YMMV.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 05:18
Joined
Sep 12, 2006
Messages
15,727
its at times like this you miss a native pointer data type

manipulating arrays is heavy computationally. if there are only a few items it is not so bad, but large arrays could take a lot of sorting.
 

Never Hide

Registered User.
Local time
Today, 07:18
Joined
Dec 22, 2011
Messages
96
mdlueck thnaks a lot for the links, I'l take a look at them :)

gemma-the-husky, the reason I'm using arrays is because the arrays that'll be generated I always going to be like 10, or maybe 20 rows high (@ worst). If it would have been a large amount of data of course I'd use a different approach :)
 
Last edited:

jdraw

Super Moderator
Staff member
Local time
Today, 00:18
Joined
Jan 23, 2006
Messages
15,403
Although you say we should trust that this is the best solution for you, the use of the array seems a long way around your basic issue.

It may just be the English but
The 1st column is the duration of the stay
is a Date and so it is NOT a duration. It may very well be the DepartureDate. And it appears you have a Business fact that says No Room Cleaning on Sunday.
So if Departure is on a Sunday, set the Cleaning of that Room to Monday. And as Galaxiom suggested, using a recordset may be the easiest technique.

However, if you insist on an array, there is an old technique using XOR 3 times to do a swap. I don't know if it applies to your situation, but here is a link to the concept/approach. (I had to use this at university many years ago)
http://en.wikipedia.org/wiki/XOR_swap_algorithm

Maybe a single XOR with a value of '00000001' ?????

Perhaps if we knew more of why the array was the "best approach", more suggestions would evolve.
 

Users who are viewing this thread

Top Bottom