how to save/restore current array position?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Magnus Warker

    #1

    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



  • Shelly

    #2
    Re: how to save/restore current array position?


    "Magnus Warker" <wagner@abwesen d.de> wrote in message
    news:dc05i8$3bj $04$1@news.t-online.com...[color=blue]
    > 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
    >[/color]

    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


    Comment

    • Magnus Warker

      #3
      Re: how to save/restore current array position?

      Hi Shelly,

      thank you very much!
      [color=blue]
      > 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[/color]

      Which variable are you talking about? There is no "next page". The problem
      code is executed at once.
      [color=blue]
      > 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.[/color]

      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

      Comment

      • Shelly

        #4
        Re: how to save/restore current array position?


        "Magnus Warker" <wagner@abwesen d.de> wrote in message
        news:dc09ge$j4l $00$1@news.t-online.com...[color=blue]
        > Hi Shelly,
        >
        > thank you very much!
        >[color=green]
        >> 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[/color]
        >
        > Which variable are you talking about? There is no "next page". The problem
        > code is executed at once.[/color]

        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 {
        }
        [color=blue]
        >[color=green]
        >> 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.[/color]
        >
        > 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.[/color]

        class theDef {
        var pos;
        }

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

        while (blah) do {
        stuff
        $i++;
        theObj->setpos($i);
        }


        Shelly


        Comment

        • Magnus Warker

          #5
          Re: how to save/restore current array position?

          Dear Shelly,

          thank you again for giving hints! .-)
          [color=blue]
          > 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 {
          > }[/color]

          I think there is a misunderstandin g 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

          Comment

          • Shelly

            #6
            Re: how to save/restore current array position?


            "Magnus Warker" <wagner@abwesen d.de> wrote in message
            news:dc0cu5$nap $03$1@news.t-online.com...[color=blue]
            > Dear Shelly,
            >
            > thank you again for giving hints! .-)[/color]

            You are welcome. I was at your stage in PHP just a short while ago.
            [color=blue][color=green]
            >> 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 {
            >> }[/color]
            >
            > I think there is a misunderstandin g 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?[/color]

            Not as far as I know.
            [color=blue]
            > So storing a value in $POST['the_index'] means storing the key of the
            > current item.[/color]

            You get the value of a variable in $_POST.
            [color=blue]
            > In analogy, retrieving a value from $POST['the_index'] means retrieving
            > the
            > key of the item which was the current item some time ago.[/color]

            ??? $_POST gets the value of a field on the form..... and it is $_POST, not
            $POST.
            [color=blue]
            > 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?[/color]

            I guess. Your language is confusing me.
            [color=blue]
            > My problem is not storing and retrieving some values, but modifying the
            > internal state of an array.
            >
            > See what I mean?[/color]

            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
            [color=blue]
            >
            > Magnus[/color]


            Comment

            • Magnus Warker

              #7
              Re: how to save/restore current array position?

              Dear Shelly!
              [color=blue]
              > You are welcome. I was at your stage in PHP just a short while ago.[/color]
              [color=blue][color=green]
              >> See what I mean?[/color][/color]
              [color=blue]
              > 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?[/color]

              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

              Comment

              • Steve

                #8
                Re: how to save/restore current array position?

                [color=blue]
                > 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.[/color]

                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

                Comment

                Working...