473,473 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to save/restore current array position?

Hi,

I want to traverse an (associative) array, starting from its current
position, until a certain element is reached. Then, in certain cases, I
want to be able to reset the current position of the array to the one that
we had before touching the array.

I. e., i need something like funcctions getpos and setpos:

----------

$pos = getpos ($list);

do
{
$item = current ($list);
$name = key ($list);

next ($list);
} while (!isWanted ($item));

setpos ($list,$pos);

----------

What I really need in the above example is:
Check, if we *could* get another wanted item from the list, but actually
leave the list (its current position) untouched.

Any ideas?

Many thanks,
Markus

Jul 24 '05 #1
7 3191

"Magnus Warker" <wa****@abwesend.de> wrote in message
news:dc*************@news.t-online.com...
Hi,

I want to traverse an (associative) array, starting from its current
position, until a certain element is reached. Then, in certain cases, I
want to be able to reset the current position of the array to the one that
we had before touching the array.

I. e., i need something like funcctions getpos and setpos:

----------

$pos = getpos ($list);

do
{
$item = current ($list);
$name = key ($list);

next ($list);
} while (!isWanted ($item));

setpos ($list,$pos);

----------

What I really need in the above example is:
Check, if we *could* get another wanted item from the list, but actually
leave the list (its current position) untouched.

Any ideas?

Many thanks,
Markus


Reverse the while and do so that it is a pre-test? As for the setpos and
getpos, there are many ways.

C style:
(1) Set a session variable with the value and read the value.
(2) Pass the variable to the next page and use $_GET

OO style:
encapsulate in the class as a variable in the class. The obect then has
the value of "pos", and getpos and setpos are merely accessor and mutator
methods of the class.

I hope that helps.

Shelly
Jul 24 '05 #2
Hi Shelly,

thank you very much!
C style:
(1) Set a session variable with the value and read the value.
(2) Pass the variable to the next page and use $_GET
Which variable are you talking about? There is no "next page". The problem
code is executed at once.
OO style:
encapsulate in the class as a variable in the class. The obect then has
the value of "pos", and getpos and setpos are merely accessor and mutator
methods of the class.


I think I cannot follow. Even when encapsulating the array in an additional
class, the original problem still remains.

What I already tried is to just do the while on a copy of the array, but
copying resets the position.

Thanks,
Magnus
Jul 24 '05 #3

"Magnus Warker" <wa****@abwesend.de> wrote in message
news:dc*************@news.t-online.com...
Hi Shelly,

thank you very much!
C style:
(1) Set a session variable with the value and read the value.
(2) Pass the variable to the next page and use $_GET
Which variable are you talking about? There is no "next page". The problem
code is executed at once.


OK, that is simpler. Here is a suggestion. Set a hidden variable as
"the_index" with a value equal to zero. Then as your index changes, set the
value of the_index to that value. Now you begin the loop at the value of
the_index with a pre-test.

$pos = $POST['the_index'] or $pos = $the_index. (I'm not sure which)
while (blah) do {
}
OO style:
encapsulate in the class as a variable in the class. The obect then
has
the value of "pos", and getpos and setpos are merely accessor and mutator
methods of the class.


I think I cannot follow. Even when encapsulating the array in an
additional
class, the original problem still remains.

What I already tried is to just do the while on a copy of the array, but
copying resets the position.


class theDef {
var pos;
}

$theObj = new theObj();
$i = $theObj->getpos();

while (blah) do {
stuff
$i++;
theObj->setpos($i);
}
Shelly
Jul 24 '05 #4
Dear Shelly,

thank you again for giving hints! .-)
OK, that is simpler. Here is a suggestion. Set a hidden variable as
"the_index" with a value equal to zero. Then as your index changes, set
the
value of the_index to that value. Now you begin the loop at the value of
the_index with a pre-test.

$pos = $POST['the_index'] or $pos = $the_index. (I'm not sure which)
while (blah) do {
}


I think there is a misunderstanding here:

I have an associative array, and my understanding of this kind of creature
is that we cannot access elements by a numerical index, right?

So storing a value in $POST['the_index'] means storing the key of the
current item.

In analogy, retrieving a value from $POST['the_index'] means retrieving the
key of the item which was the current item some time ago.

However, having the key of the item which was the current item some time ago
does not enable me to set the internal pointer of the "current" item within
the array, does it?

My problem is not storing and retrieving some values, but modifying the
internal state of an array.

See what I mean?

Magnus
Jul 24 '05 #5

"Magnus Warker" <wa****@abwesend.de> wrote in message
news:dc*************@news.t-online.com...
Dear Shelly,

thank you again for giving hints! .-)
You are welcome. I was at your stage in PHP just a short while ago.
OK, that is simpler. Here is a suggestion. Set a hidden variable as
"the_index" with a value equal to zero. Then as your index changes, set
the
value of the_index to that value. Now you begin the loop at the value of
the_index with a pre-test.

$pos = $POST['the_index'] or $pos = $the_index. (I'm not sure
which)
while (blah) do {
}


I think there is a misunderstanding here:

I have an associative array, and my understanding of this kind of creature
is that we cannot access elements by a numerical index, right?


Not as far as I know.
So storing a value in $POST['the_index'] means storing the key of the
current item.
You get the value of a variable in $_POST.
In analogy, retrieving a value from $POST['the_index'] means retrieving
the
key of the item which was the current item some time ago.
??? $_POST gets the value of a field on the form..... and it is $_POST, not
$POST.
However, having the key of the item which was the current item some time
ago
does not enable me to set the internal pointer of the "current" item
within
the array, does it?
I guess. Your language is confusing me.
My problem is not storing and retrieving some values, but modifying the
internal state of an array.

See what I mean?
No. What do you mean by "modifying the internal state of an array."?
Changing values in the array? Dropping items from the array by unset() ing
them? What **do** you mean?

Shelly

Magnus

Jul 24 '05 #6
Dear Shelly!
You are welcome. I was at your stage in PHP just a short while ago.
See what I mean?

No. What do you mean by "modifying the internal state of an array."?
Changing values in the array? Dropping items from the array by unset()
ing
them? What **do** you mean?


If you don't understand the problem, you should take a look at the array
functions 'current', 'reset' and 'next'. They all change the internal
position of an array.

Nevertheless, thanks for replying to my question,
Magnus
Jul 25 '05 #7
I want to traverse an (associative) array, starting from its current
position, until a certain element is reached. Then, in certain cases, I
want to be able to reset the current position of the array to the one that
we had before touching the array.


Even associative arrays have internal numeric keys which (a) are used
by prev(), next() to order the retrieval and (b) can be used to set the
current array pointer randomly.

array_keys() creates an array of the internal numeric keys that can be
used to navigate the array instead of using prev(), next() etc. That
way you have complete control over which element to access next.
$test = array();

$test[] = 'One';
$test[] = 'Two';
$test[] = 'Three';
$test[] = 'Four';
$test[] = 'Five';
$test[] = 'Six';

$keys = array_keys( $test );

$ptr = reset( $keys );
print $test[ $ptr ] . "\n";

$ptr = next( $keys );
print $test[ $ptr ] . "\n";

$ptr = next( $keys );
print $test[ $ptr ] . "\n";

print "remembering...\n";

$remember = $ptr;

$ptr = next( $keys );
print $test[ $ptr ] . "\n";

$ptr = next( $keys );
print $test[ $ptr ] . "\n";

print "recalling...\n";

$ptr = $remember;

reset( $keys );
while( $ptr < next( $keys ) );

$ptr = next( $keys );
print $test[ $ptr ] . "\n";

$ptr = next( $keys );
print $test[ $ptr ] . "\n";
// output...

One
Two
Three
remembering...
Four
Five
recalling...
Three
Four

---
Steve

Jul 25 '05 #8

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

Similar topics

1
by: Dave Romig | last post by:
Can anyone provide guidance for saving and restoring an XMLDOM object as a binary blob? In a VB application, I need to repeatedly save and restore a large, dynamic XML document. Currently, the...
2
by: Kevin Zhou | last post by:
I have a file that contains 22 lines, each line has two float separated by a space. I want to store those float data into a two-dimensional array which size I want the program to determine at run...
4
by: Eomer | last post by:
How do i save an int value or an array to a .txt file? which file to include? what is the function? what is the syntax? what are the common problems? Thanks in advance, Eomer
14
by: fdu.xiaojf | last post by:
Hi, I have a program which will continue to run for several days. When it is running, I can't do anything except waiting because it takes over most of the CUP time. Is it possible that the...
7
by: newscorrespondent | last post by:
I want to save the state of a splitcontainer when an application ends. I also save the form state. The SplitContainer does not seem to have a well defined place to put intiialization and...
3
by: Angus | last post by:
I have a web page with a toolbar containing a Save button. The Save button can change contextually to be a Search button in some cases. Hence the button name searchsavechanges. The snippet of...
0
by: Mirover | last post by:
how can I use the "result set " (fill a temp table) with the returned result set of querys like this : restore filelistonly,restore headeronly...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.