Re: [PHP] How do I start at a specific position in an array? Is there
Thomas Goyne wrote: On Sun, 09 Jan 2005 15:59:43 +0100, M. Sokolewicz [EMAIL PROTECTED] wrote: that's not a SPECIFIC place in the array, that's just current, next and previous. AFAIK there is no way to explicitly set the internal pointer of the array to a spcified place. I used a function which basically looped trough the array until it got to the correct depth, and then returned it by referencebut it's not very efficient :S Why would you ever want to do that other than to waste cycles? I'll give you a simple case. I have a GTK PHP MP3 ID3 editor application I'm working on. When one opens a file in a directory, I provide next/prev buttons to quickly page to the next/prev file in the directory. Getting the next/prev to work is simple enough, but... I've got the whole array built from opendir/readdir, and sorted it into alphabetical order. I've got a filename inside that array, from the pre-defined GtkFileSelection dialog. So I need to initialize the internal array pointer to the position of the file within that array, there is no such PHP function. See also: http://bugs.php.net/bug.php?id=31375 -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How do I start at a specific position in an array? Is there
On Mon, 2005-01-10 at 11:28, Richard Lynch wrote: Thomas Goyne wrote: On Sun, 09 Jan 2005 15:59:43 +0100, M. Sokolewicz [EMAIL PROTECTED] wrote: that's not a SPECIFIC place in the array, that's just current, next and previous. AFAIK there is no way to explicitly set the internal pointer of the array to a spcified place. I used a function which basically looped trough the array until it got to the correct depth, and then returned it by referencebut it's not very efficient :S Why would you ever want to do that other than to waste cycles? I'll give you a simple case. I have a GTK PHP MP3 ID3 editor application I'm working on. When one opens a file in a directory, I provide next/prev buttons to quickly page to the next/prev file in the directory. Getting the next/prev to work is simple enough, but... I've got the whole array built from opendir/readdir, and sorted it into alphabetical order. I've got a filename inside that array, from the pre-defined GtkFileSelection dialog. So I need to initialize the internal array pointer to the position of the file within that array, there is no such PHP function. Why not change your structure? Sounds like you want a linked list, better yet a doubly linked list. So create a double linked list class, link up your entries, and keep a hash available for fast lookup into the linked list where the keys are the filenames or whatever you want to use as an index, and the value is a reference to the interesting linked list node. Given that scenario you will have a currNode object which is the currently selected song, and when someone moves up or down the list you just do something like: if( $currNode-hasNext() ) { $currNode = $currNode-next(); } Moving backward would be as simple as: if( $currNode-hasPrevious() ) { $currNode = $currNode-previous(); } And if the user selects an arbitrary file: $currNode = $lookupArray[$index]; This kind of structure works a lot like a doubly threaded red-black tree :) Although a red-black tree would keep your entries in sorted order whenever you add new entries in O( lg n ) time. Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How do I start at a specific position in an array? Is there
Robert Cummings wrote: On Mon, 2005-01-10 at 11:28, Richard Lynch wrote: Thomas Goyne wrote: On Sun, 09 Jan 2005 15:59:43 +0100, M. Sokolewicz [EMAIL PROTECTED] wrote: that's not a SPECIFIC place in the array, that's just current, next and previous. AFAIK there is no way to explicitly set the internal pointer of the array to a spcified place. I used a function which basically looped trough the array until it got to the correct depth, and then returned it by referencebut it's not very efficient :S Why would you ever want to do that other than to waste cycles? I'll give you a simple case. I have a GTK PHP MP3 ID3 editor application I'm working on. When one opens a file in a directory, I provide next/prev buttons to quickly page to the next/prev file in the directory. Getting the next/prev to work is simple enough, but... I've got the whole array built from opendir/readdir, and sorted it into alphabetical order. I've got a filename inside that array, from the pre-defined GtkFileSelection dialog. So I need to initialize the internal array pointer to the position of the file within that array, there is no such PHP function. Why not change your structure? Sounds like you want a linked list, better yet a doubly linked list. So create a double linked list class, link up your entries, and keep a hash available for fast lookup into the linked list where the keys are the filenames or whatever you want to use as an index, and the value is a reference to the interesting linked list node. Uhhhm. Yeah. I could spend hours finding/writing a doubly-linked list class, which will be a good bit slower and WAY more memory-bloated than the built-in PHP doubly-linked list (aka 'array') and, in theory, I could code it so that the 'set_current' function existed and was maybe a bit faster than walking the PHP array... But every *other* operation would be a lot slower, and I only need to walk the list to initialize the internal current pointer once, when the user is already distracted and slowed down by the Open File dialog. I think the user experience will be *much* better without all that, though. if( $currNode-hasNext() ) { $currNode = $currNode-next(); } if ($current != $array[count($array) - 1]) $current = next($array); Moving backward would be as simple as: if( $currNode-hasPrevious() ) { $currNode = $currNode-previous(); } if ($current != $array[0]) $current = prev($array); Actually, the buttons are disabled if you are on the first/last item, so the checks for hasNext/hasPrevious are done elsewhere anyway. And if the user selects an arbitrary file: $currNode = $lookupArray[$index]; No, no, no. The FileSelection already returns the filename. Of course, I just *built* that array based on the directory they chose, so could initialize the $current_index as the array got built... So I'd need to keep an *INVERSE* array internally to do: $current_index = $inversearray[$filename]; So then I'd be keeping track of the $current_index, at which point I'd not even *NEED* to bother with next/prev and friends: I'd just have a global variable for the integer index in the array, and play with that in PHP. No bloated class objects to track something as simple as an integer from 0 to (count($array) - 1) Maybe I'll just ditch the internal prev/next stuff and use $CURRENT_INDEX as a global everywhere and write my own prev/next functions to do integer arithmetic instead. Seems kinda silly when it's all already built-in to PHP *except* set_current() but there ya go. This kind of structure works a lot like a doubly threaded red-black tree :) Although a red-black tree would keep your entries in sorted order whenever you add new entries in O( lg n ) time. You're comparing O(lg n) in PHP to O(lg n) in built-in C from PHP. Not the same at all until we start hitting the kind of numbers that aren't even useful for the number of files in a single directory. I'm *not* going to set this box up to have 10,000 files in the same directory. Puhlease! The only time something is added to my tree is when they open up a new directory, at which point the whole tree is re-built from scratch, since it will have zero (0) elements in common with the previous tree. As much as I love Object-Oriented programming, it amazes me what lengths people will go to in order to use it no matter how inappropriate it may be to the task at hand. :-) -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How do I start at a specific position in an array? Is there
Richard Lynch wrote: I'll give you a simple case. I have a GTK PHP MP3 ID3 editor application I'm working on. When one opens a file in a directory, I provide next/prev buttons to quickly page to the next/prev file in the directory. Getting the next/prev to work is simple enough, but... I've got the whole array built from opendir/readdir, and sorted it into alphabetical order. I've got a filename inside that array, from the pre-defined GtkFileSelection dialog. So I need to initialize the internal array pointer to the position of the file within that array, there is no such PHP function. for(;;) loop would not work? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How do I start at a specific position in an array? Is there
On Mon, 2005-01-10 at 12:44, Richard Lynch wrote: Robert Cummings wrote: On Mon, 2005-01-10 at 11:28, Richard Lynch wrote: Thomas Goyne wrote: On Sun, 09 Jan 2005 15:59:43 +0100, M. Sokolewicz [EMAIL PROTECTED] wrote: that's not a SPECIFIC place in the array, that's just current, next and previous. AFAIK there is no way to explicitly set the internal pointer of the array to a spcified place. I used a function which basically looped trough the array until it got to the correct depth, and then returned it by referencebut it's not very efficient :S Why would you ever want to do that other than to waste cycles? I'll give you a simple case. I have a GTK PHP MP3 ID3 editor application I'm working on. When one opens a file in a directory, I provide next/prev buttons to quickly page to the next/prev file in the directory. Getting the next/prev to work is simple enough, but... I've got the whole array built from opendir/readdir, and sorted it into alphabetical order. I've got a filename inside that array, from the pre-defined GtkFileSelection dialog. So I need to initialize the internal array pointer to the position of the file within that array, there is no such PHP function. Why not change your structure? Sounds like you want a linked list, better yet a doubly linked list. So create a double linked list class, link up your entries, and keep a hash available for fast lookup into the linked list where the keys are the filenames or whatever you want to use as an index, and the value is a reference to the interesting linked list node. Uhhhm. Yeah. I could spend hours finding/writing a doubly-linked list class, which will be a good bit slower and WAY more memory-bloated than the built-in PHP doubly-linked list (aka 'array') and, in theory, I could code it so that the 'set_current' function existed and was maybe a bit faster than walking the PHP array... But every *other* operation would be a lot slower, and I only need to walk the list to initialize the internal current pointer once, when the user is already distracted and slowed down by the Open File dialog. I think the user experience will be *much* better without all that, though. if( $currNode-hasNext() ) { $currNode = $currNode-next(); } if ($current != $array[count($array) - 1]) $current = next($array); Moving backward would be as simple as: if( $currNode-hasPrevious() ) { $currNode = $currNode-previous(); } if ($current != $array[0]) $current = prev($array); Actually, the buttons are disabled if you are on the first/last item, so the checks for hasNext/hasPrevious are done elsewhere anyway. And if the user selects an arbitrary file: $currNode = $lookupArray[$index]; No, no, no. The FileSelection already returns the filename. Of course, I just *built* that array based on the directory they chose, so could initialize the $current_index as the array got built... So I'd need to keep an *INVERSE* array internally to do: $current_index = $inversearray[$filename]; So then I'd be keeping track of the $current_index, at which point I'd not even *NEED* to bother with next/prev and friends: I'd just have a global variable for the integer index in the array, and play with that in PHP. No bloated class objects to track something as simple as an integer from 0 to (count($array) - 1) Maybe I'll just ditch the internal prev/next stuff and use $CURRENT_INDEX as a global everywhere and write my own prev/next functions to do integer arithmetic instead. Seems kinda silly when it's all already built-in to PHP *except* set_current() but there ya go. This kind of structure works a lot like a doubly threaded red-black tree :) Although a red-black tree would keep your entries in sorted order whenever you add new entries in O( lg n ) time. You're comparing O(lg n) in PHP to O(lg n) in built-in C from PHP. Not the same at all until we start hitting the kind of numbers that aren't even useful for the number of files in a single directory. I'm *not* going to set this box up to have 10,000 files in the same directory. Puhlease! The only time something is added to my tree is when they open up a new directory, at which point the whole tree is re-built from scratch, since it will have zero (0) elements in common with the previous tree. As much as I love Object-Oriented programming, it amazes me what lengths people will go to in order to use it no matter how inappropriate it may be to the task at hand. :-) I was going to write a lengthy response to your berating, but then I remembered you're a procedural zealot and the above comments aren't much better than the bait on the end of a troll's line. Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com |
Re: [PHP] How do I start at a specific position in an array? Is there
Marek Kilimajer wrote: Richard Lynch wrote: I'll give you a simple case. I have a GTK PHP MP3 ID3 editor application I'm working on. When one opens a file in a directory, I provide next/prev buttons to quickly page to the next/prev file in the directory. Getting the next/prev to work is simple enough, but... I've got the whole array built from opendir/readdir, and sorted it into alphabetical order. I've got a filename inside that array, from the pre-defined GtkFileSelection dialog. So I need to initialize the internal array pointer to the position of the file within that array, there is no such PHP function. for(;;) loop would not work? Yes, and since the array is small it's no big deal -- I already have that working. It just would be more clean to have a set_current function to set the internal array iterator to the correct position. -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How do I start at a specific position in an array? Is there a function?
Brent Clements wrote / napísal (a): I know this is a simple question because I could easily write a loop to move to the specific position in the array, but I want to know is there a function to move the array pointer position to a specific position in the array? Thanks, Brent next($array) prev($array) current($array) trobi -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How do I start at a specific position in an array? Is there
Tr wrote: Brent Clements wrote / napísal (a): I know this is a simple question because I could easily write a loop to move to the specific position in the array, but I want to know is there a function to move the array pointer position to a specific position in the array? Thanks, Brent next($array) prev($array) current($array) trobi that's not a SPECIFIC place in the array, that's just current, next and previous. AFAIK there is no way to explicitly set the internal pointer of the array to a spcified place. I used a function which basically looped trough the array until it got to the correct depth, and then returned it by referencebut it's not very efficient :S -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How do I start at a specific position in an array? Is there
On Sun, 09 Jan 2005 15:59:43 +0100, M. Sokolewicz [EMAIL PROTECTED] wrote: that's not a SPECIFIC place in the array, that's just current, next and previous. AFAIK there is no way to explicitly set the internal pointer of the array to a spcified place. I used a function which basically looped trough the array until it got to the correct depth, and then returned it by referencebut it's not very efficient :S Why would you ever want to do that other than to waste cycles? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How do I start at a specific position in an array? Is there
Thomas Goyne wrote: On Sun, 09 Jan 2005 15:59:43 +0100, M. Sokolewicz [EMAIL PROTECTED] wrote: that's not a SPECIFIC place in the array, that's just current, next and previous. AFAIK there is no way to explicitly set the internal pointer of the array to a spcified place. I used a function which basically looped trough the array until it got to the correct depth, and then returned it by referencebut it's not very efficient :S Why would you ever want to do that other than to waste cycles? can't recall what it was exactly, but I believe it was a case where I didn't know the key of the array, nor the value, but knew the place in the array. Can't exactly remember what it was... I am now thinking that what I'm saying in my last line can be done easily by using array_values and using a numeric key But... as I said, I can't recall what it was. In the end I simply rewrote whatever it was to use a more efficient way of handling things -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How do I start at a specific position in an array? Is there
On Sun, 09 Jan 2005 23:58:18 +0100, M. Sokolewicz [EMAIL PROTECTED] wrote: can't recall what it was exactly, but I believe it was a case where I didn't know the key of the array, nor the value, but knew the place in the array. Can't exactly remember what it was... I am now thinking that what I'm saying in my last line can be done easily by using array_values and using a numeric key But... as I said, I can't recall what it was. In the end I simply rewrote whatever it was to use a more efficient way of handling things array_keys($arr)[$place] will get you the key for the current place in the array. -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ http://www.smempire.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] How do I start at a specific position in an array? Is there a function?
I know this is a simple question because I could easily write a loop to move to the specific position in the array, but I want to know is there a function to move the array pointer position to a specific position in the array? Thanks, Brent -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php