Re: counting with leading zeros

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2007-09-26 at 15:58 -0400, brian wrote:
> Robert Cummings wrote:
> > On Wed, 2007-09-26 at 15:11 -0400, brian wrote:
> > 
> >>I have a directory that contains many images (no, not pr0n, 
> >>unfortunately) and i'm working on an admin script for adding to it. I've 
> >>got something that works alright but i'm wondering if there's a Better Way.
> >>
> >>Each image is named like: foo_01.jpg, foo_02.jpg, bar_01.jpg, and so on. 
> >>When adding a new image it should be assigned the next number in the 
> >>series (the prefix is known). Thus, given the prefix 'bar' i do 
> >>something like:
> >>
> >>function getNextImage($path, $prefix)
> >>{
> >>   $pattern = "/^${prefix}_([0-9]{2})\.[a-z]{3}$/";
> >>
> >>   $filenames = glob("${path}${prefix}*");
> >>	
> >>   if (is_array($filenames) && sizeof($filenames))
> >>   {
> >>     sort($filenames);
> >>
> >>     /* eg. 'foo_32.jpg'
> >>      */
> >>     $last = basename(array_pop($filenames));
> >>
> >>     /* pull the number from the filename
> >>      */
> >>     $count = intval(preg_replace($pattern, '$1', $last));
> >>
> >>     /* increment, format with leading zero again if necessary,
> >>      * and return it
> >>      */
> >>     return sprintf('%02d', ++$count)
> >>   }
> >>   else
> >>   {
> >>     return '01';
> >>   }
> >>}
> >>
> >>Note that there almost certainly will never be more than 99 images to a 
> >>series. In any case, i don't care about there being more than one 
> >>leading zero. One is what i want.
> > 
> > 
> > <?php
> > 
> > function getNextImageIndex( $path, $prefix )
> > {
> >     //
> >     // Matches files like foo_123.png
> >     //
> >     $pattern = '/.*_([[:digit:]]+)\.[[:alpha:]]{3}$/';
> > 
> >     $filenames = glob( "${path}${prefix}*" );
> >         
> >     if( $filenames )
> >     {
> >         sort( $filenames );
> > 
> >         $last = array_pop( $filenames );
> 
> 
> It needs basename here because the array has the full path of each file.

No it doesn't. I changed the pattern to make it simpler.


> >         $nextIndex = (int)preg_replace( $pattern, '$1', $last ) + 1;
> > 
> >         return str_pad( $nextIndex, 2, '0', STR_PAD_LEFT );
> >     }
> > 
> >     return '01';
> > }
> > 
> > ?>
> 
> 
> Well, this is almost precisely the same thing i have, save for using 
> POSIX character classes, str_pad instead of sprintf(), and incrementing 
> elsewhere. What i was really wondering is if there was a *much simpler* 
> way to do this, not just re-arranging things.

You asked for a "Better Way". I gave you a "Better Way". I improved the
regex, removed the useless basename() call, performed incrementation in
the extraction step, used better function and variable names, and used
the str_pad() function because sprintf() is overkill. I also removed
your redundant else clause. My version is much simpler. How much is
much? You know... much! Other than using a database, it doesn't really
get any simpler. Although you obviously have race condition issues also.
But that's not getting "simpler". It's getting more correct but also a
bit more complex.

> > Note that the moment you get 100 images the code breaks because _100
> > sorts before _99 and so you will always write over the 100th index. To
> > fix this you need to loop through all found files and pull the index out
> > and record the highest found.
> 
> For my purposes, if any series gets much beyond 40 i'll have bigger 
> problems to worry about. I'm content to not worry about overflow in this 
> case.

Suit yourself. But better programmer's don't just wave their hands in
the air and hope for the best.

Cheers,
Rob.
-- 
...........................................................
SwarmBuy.com - http://www.swarmbuy.com

    Leveraging the buying power of the masses!
...........................................................

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux