Re: [PHP] counting with leading zeros

2007-10-01 Thread Martin Alterisio
// glob returns an ordered list (rtfm: man glob)
// relies on the use of the operator ++ to generate string sequences.

function lastFileInSequence($path, $prefix, $first, $suffix) {
return array_pop(glob($path . / . $prefix . str_repeat('?',
strlen($first)) . $suffix));
}

function nextFileInSequence($path, $prefix, $first, $suffix) {
if ($lastFile = basename(lastFileInSequence($path, $prefix, $first,
$suffix), $suffix)) {
return ++$lastFile . $suffix;
} else {
return $prefix . $first . $suffix;
}
}

$nextImage = $path . / . nextFileInSequence($path, foo_, 01, .jpg);

2007/9/26, brian [EMAIL PROTECTED]:

 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.

 brian

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




Re: [PHP] counting with leading zeros

2007-09-30 Thread Robert Cummings
On Sat, 2007-09-29 at 23:47 -0400, brian wrote:

 It's not so much that i disrespect Rob. It's just that i felt i had to 
 call him out for being a dickhead by taking my statement out of context.

Ok, so I'm a dickhead. What does that make you? A vagina?

Name calling really is the lowest form of argumentation. It was fun when
I was 7. So were swear words.

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



Re: [PHP] counting with leading zeros

2007-09-30 Thread Jeffery Fernandez
gee you guys behave like kids. don't you all have other things to do than 
whinge, swear  and blame each other on the list? Take it elsewhere and sort 
it out. This is bad for the PHP community in general. It doesn't reflect what 
the community is about.

my 2c.

On Sunday 30 September 2007 13:47, brian wrote:
 tedd wrote:
  At 8:30 PM -0400 9/27/07, brian wrote:
  I wasn't bitching! And i *thought* that my numerous attempts at
  explaining such would have been enough. Obviously not, but wtf can you
  do with people who seem to want only to stir up shit?
 
  It sure sounded like you were bitching.

 Gee, thanks for pointing that out, Jeeves. I'd say it's pretty damned
 obvious now that Rob thought i was bitching about his response to my
 query. However, if you'd have bothered to note my replies since, you
 might have twigged to the fact that i'd been trying to set that
 straight. But you just *had* to send off your own dickish response.

  And, your off-list fuck you, too, asswipe to me certainly seems to
  support that -- don't you think?

 How so? Had it crossed your little mind that maybe--just maybe--i was
 becoming not a little frustrated with the fact that my reply had been
 taken the wrong way? That, instead of accepting that and moving on, this
 had become far too OT with bullshit comments such as yours? So, yeah,
 you might say that i'm bitching *now*.

 You and i have had our run-ins on this list on more than one occasion.
 Until now, i've chosen to take your crap in stride. If you have nothing
 constructive to add then STFU.

  Look, there's nothing you can say to me that hasn't been said before
  (some of it deserving), so my advice is for you to refrain from such
  conduct and at least try to look professional. That way you'll hide your
  ignorance a little longer -- it's always worked for me.

 This is me laughing. I'm sure you have no bloody idea how funny that
 statement looks from where i'm sitting. See the paragraph above.

  Dan Parry wrote:
  Can I please interject and say that I vastly respect Tedd(ddd) and
  Rob(bb?) and their opinions

 It's not so much that i disrespect Rob. It's just that i felt i had to
 call him out for being a dickhead by taking my statement out of context.

 Tedd ... not so very much at all.

 Robert Cummings wrote:
   On Fri, 2007-09-28 at 12:05 -0400, tedd wrote:
  At 10:39 PM -0400 9/26/07, brian wrote:
  
  fuck you, too, asswipe
  
   Seems to be, an extra, comma, in the phrase. It reads, like Captain
   Kirk, saying, it.

 Funny. Reminds of a Capt. Kirk quote:
 No more blah, blah, blah!

 brian

-- 
Powered by openSUSE 10.2 (i586) Kernel: 2.6.18.8-0.5-default
KDE: 3.5.5 release 45.4
 10:55pm  up 10 days  2:22,  3 users,  load average: 0.13, 0.18, 0.17

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



Re: [PHP] counting with leading zeros

2007-09-30 Thread tedd

At 11:47 PM -0400 9/29/07, brian wrote:
You and i have had our run-ins on this list on more than one 
occasion. Until now, i've chosen to take your crap in stride.


brian

I haven't noticed any run-ins. I don't care if you've been the 
biggest prick on the net -- if you ask a question and it's within my 
scope to answer, I'll try to answer -- that's what this list is 
about. I seldom remember who said what, nor do I keep score -- I 
haven't got time for that.


As for taking crap, that's your perception for I don't dish any 
out. If you take offense to anything I say, then say so and I'll 
explain myself -- I'm not here to insult anyone. I haven't got time 
for that either.



 If you have nothing constructive to add then STFU.


If I do shut up, or not, will be my decision and not your's. You seem 
to have an attitude that the net is yours and our time is here for 
you to use and abuse at your will. Clearly, you have some unrealized 
issues -- but, that's your problem to work out.


However, when I see someone try to help another and see that it's not 
appreciated, then I can make comment if I want because it IS 
constructive to point out civility on the net. Telling someone to 
fuck-off or calling someone a dick-head is certainly not constructive 
and degrades the professionalism of the list. That should be obvious.


If you want to be treated fairly, then treat others that way. Don't 
always assume that you're right and everyone else is wrong and thus 
you have liberty to call people names because that doesn't promote an 
exchange of information, which is what this list is about. It's 
clear, at least to me, that you have problems with that as well.


So, why don't we let this thread drop -- you called everyone names 
and used whatever language you thought appropriate and I hope you 
feel better now. Hopefully next time, we can all be more civil to 
each other.


Cheers,

tedd



Look, there's nothing you can say to me that hasn't been said 
before (some of it deserving), so my advice is for you to refrain 
from such conduct and at least try to look professional. That way 
you'll hide your ignorance a little longer -- it's always worked 
for me.


This is me laughing. I'm sure you have no bloody idea how funny that 
statement looks from where i'm sitting. See the paragraph above.



Dan Parry wrote:

Can I please interject and say that I vastly respect Tedd(ddd) and Rob(bb?)
and their opinions



It's not so much that i disrespect Rob. It's just that i felt i had 
to call him out for being a dickhead by taking my statement out of 
context.


Tedd ... not so very much at all.

Robert Cummings wrote:

 On Fri, 2007-09-28 at 12:05 -0400, tedd wrote:


At 10:39 PM -0400 9/26/07, brian wrote:


fuck you, too, asswipe


 Seems to be, an extra, comma, in the phrase. It reads, like Captain
 Kirk, saying, it.



Funny. Reminds of a Capt. Kirk quote:
No more blah, blah, blah!

brian

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



--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

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



Re: [PHP] counting with leading zeros

2007-09-29 Thread brian

tedd wrote:

At 8:30 PM -0400 9/27/07, brian wrote:


I wasn't bitching! And i *thought* that my numerous attempts at
explaining such would have been enough. Obviously not, but wtf can you
do with people who seem to want only to stir up shit?


It sure sounded like you were bitching.


Gee, thanks for pointing that out, Jeeves. I'd say it's pretty damned 
obvious now that Rob thought i was bitching about his response to my 
query. However, if you'd have bothered to note my replies since, you 
might have twigged to the fact that i'd been trying to set that 
straight. But you just *had* to send off your own dickish response.


And, your off-list fuck you, too, asswipe to me certainly seems to 
support that -- don't you think?


How so? Had it crossed your little mind that maybe--just maybe--i was 
becoming not a little frustrated with the fact that my reply had been 
taken the wrong way? That, instead of accepting that and moving on, this 
had become far too OT with bullshit comments such as yours? So, yeah, 
you might say that i'm bitching *now*.


You and i have had our run-ins on this list on more than one occasion. 
Until now, i've chosen to take your crap in stride. If you have nothing 
constructive to add then STFU.


Look, there's nothing you can say to me that hasn't been said before 
(some of it deserving), so my advice is for you to refrain from such 
conduct and at least try to look professional. That way you'll hide your 
ignorance a little longer -- it's always worked for me.


This is me laughing. I'm sure you have no bloody idea how funny that 
statement looks from where i'm sitting. See the paragraph above.



Dan Parry wrote:

Can I please interject and say that I vastly respect Tedd(ddd) and Rob(bb?)
and their opinions



It's not so much that i disrespect Rob. It's just that i felt i had to 
call him out for being a dickhead by taking my statement out of context.


Tedd ... not so very much at all.

Robert Cummings wrote:
 On Fri, 2007-09-28 at 12:05 -0400, tedd wrote:

At 10:39 PM -0400 9/26/07, brian wrote:

fuck you, too, asswipe

 Seems to be, an extra, comma, in the phrase. It reads, like Captain
 Kirk, saying, it.


Funny. Reminds of a Capt. Kirk quote:
No more blah, blah, blah!

brian

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



Re: [PHP] counting with leading zeros

2007-09-28 Thread tedd

At 8:30 PM -0400 9/27/07, brian wrote:

I wasn't bitching! And i *thought* that my numerous attempts at
explaining such would have been enough. Obviously not, but wtf can you
do with people who seem to want only to stir up shit?


It sure sounded like you were bitching.

And, your off-list fuck you, too, asswipe to me certainly seems to 
support that -- don't you think?


Look, there's nothing you can say to me that hasn't been said before 
(some of it deserving), so my advice is for you to refrain from such 
conduct and at least try to look professional. That way you'll hide 
your ignorance a little longer -- it's always worked for me.


Cheers,

tedd
--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

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



RE: [PHP] counting with leading zeros

2007-09-28 Thread Dan Parry
Can I please interject and say that I vastly respect Tedd(ddd) and Rob(bb?)
and their opinions

Dsn

-- 
Dan Parry
Senior Developer
Virtua Webtech Ltd
 
Company Number: 5078356 
Vat Number: 827044536 
 
[EMAIL PROTECTED]
www.virtuawebtech.co.uk

01745 354924


 -Original Message-
 From: tedd [mailto:[EMAIL PROTECTED]
 Sent: 28 September 2007 17:20
 To: brian; php-general@lists.php.net
 Subject: Re: [PHP] counting with leading zeros
 
 At 8:30 PM -0400 9/27/07, brian wrote:
 I wasn't bitching! And i *thought* that my numerous attempts at
 explaining such would have been enough. Obviously not, but wtf can you
 do with people who seem to want only to stir up shit?
 
 It sure sounded like you were bitching.
 
 And, your off-list fuck you, too, asswipe to me certainly seems to
 support that -- don't you think?
 
 Look, there's nothing you can say to me that hasn't been said before
 (some of it deserving), so my advice is for you to refrain from such
 conduct and at least try to look professional. That way you'll hide
 your ignorance a little longer -- it's always worked for me.
 
 Cheers,
 
 tedd
 --
 ---
 http://sperling.com  http://ancientstones.com  http://earthstones.com
 
 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 
 
 
 --
 No virus found in this incoming message.
 Checked by AVG Free Edition.
 Version: 7.5.488 / Virus Database: 269.13.33/1034 - Release Date:
 27/09/2007 17:00

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



Re: [PHP] counting with leading zeros

2007-09-27 Thread Robert Cummings
On Wed, 2007-09-26 at 21:56 -0700, Jim Lucas wrote:

 Now, if you plan to delete any images in the list, this will not work.
 
 But if you don't expect to delete any images in the list and always want 
 the ($total_images + 1), then follow along.
 
 ?php
 
 function getNextImage($path, $prefix) {
$filelist = glob(${path}${prefix}*) or return '01';
return str_pad(((int)count($filelist)+1), 2, '0', STR_PAD_LEFT);
 }
 
 ?

Nice :)

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



Re: [PHP] counting with leading zeros

2007-09-27 Thread tedd



 Robert Cummings wrote:

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

Ah crap -- there's another technique I have to cross off my list.

Cheers,

tedd

--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

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



Re: [PHP] counting with leading zeros

2007-09-27 Thread tedd

At 3:58 PM -0400 9/26/07, brian wrote:
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.



Rob just gave you code that works better than what you did and you're 
bitching about it?


Look, none of us are on your payroll so appreciate what you're given.

My advice, says thanks and if the code doesn't meet your expectations 
then learn how to better phrase your question next time.


tedd

--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

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



Re: [PHP] counting with leading zeros

2007-09-27 Thread tedd

At 10:39 PM -0400 9/26/07, brian wrote:

Now you're obviously just trying to be a dickhead.


While someone is, it's not Rob.

Cheers,

tedd

--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

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



Re: [PHP] counting with leading zeros

2007-09-27 Thread brian

tedd wrote:

At 3:58 PM -0400 9/26/07, brian wrote:

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.


Rob just gave you code that works better than what you did and you're
 bitching about it?

Look, none of us are on your payroll so appreciate what you're given.

My advice, says thanks and if the code doesn't meet your expectations
 then learn how to better phrase your question next time.

tedd



I wasn't bitching! And i *thought* that my numerous attempts at
explaining such would have been enough. Obviously not, but wtf can you
do with people who seem to want only to stir up shit?

Jim Lucas wrote:

Well, I didn't want to get into the middle of this cat fight, but now
 that the talks have completely gotten off topic.  I will
interject...



Perfect timing, then.


Here is a function that is a bit simpler then either of yours.

Now, if you plan to delete any images in the list, this will not 
work.


There's a daemon that locks the dir, zips up the contents, moves it
elsewhere, then removes everything at once.


?php

function getNextImage($path, $prefix) { $filelist = 
glob(${path}${prefix}*) or return '01'; return 
str_pad(((int)count($filelist)+1), 2, '0', STR_PAD_LEFT); }


?


Sweet! Thanks much!

brian

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



Re: [PHP] counting with leading zeros

2007-09-26 Thread Robert Cummings
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 );
$nextIndex = (int)preg_replace( $pattern, '$1', $last ) + 1;

return str_pad( $nextIndex, 2, '0', STR_PAD_LEFT );
}

return '01';
}

?

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.

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



Re: [PHP] counting with leading zeros

2007-09-26 Thread brian

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.


$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.



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.


brian

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



Re: [PHP] counting with leading zeros

2007-09-26 Thread Robert Cummings
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



Re: [PHP] counting with leading zeros

2007-09-26 Thread brian

Robert Cummings wrote:

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.


My bad. I didn't look at it closely enough.






$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.




Bad day or something? Did it really seem that i was bitching about your 
response? If my response to that gave you the impression i was

complaining, i assure you that i wasn't. I was simply suggesting
that i was wondering if there was a *much* simpler way to do this, ie.
without using several functions to process the filename. Yes, your
version *does* have some improvements. I'm not disagreeing with you. 
Perhaps i could have been more forthright in my praise for said 
improvements (i'm even going to use some of them--how about that?!)


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.



While i agree with you in principle, i'm not interested in that aspect
of it. The application would have halted *long* before it could reach
that many. This is why i went to the trouble of pointing this out in the 
original mail. IOW, i'm not stupid; i'm well aware that 100 comes after 
99 and that it contains 3 digits, not 2, thanks very much.


b

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



Re: [PHP] counting with leading zeros

2007-09-26 Thread Robert Cummings
On Wed, 2007-09-26 at 19:10 -0400, brian wrote:
 Robert Cummings wrote:

  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.
  
 
 Bad day or something?

Nope.

 Did it really seem that i was bitching about your 
 response?

Kinda.

 If my response to that gave you the impression i was
 complaining, i assure you that i wasn't. I was simply suggesting
 that i was wondering if there was a *much* simpler way to do this, ie.
 without using several functions to process the filename. Yes, your
 version *does* have some improvements. I'm not disagreeing with you. 
 Perhaps i could have been more forthright in my praise for said 
 improvements (i'm even going to use some of them--how about that?!)

Well, I'm not looking for praise... but asking a concise question goes a
long way towards meeting your desires.

  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. 
 
 While i agree with you in principle, i'm not interested in that aspect
 of it. The application would have halted *long* before it could reach
 that many. This is why i went to the trouble of pointing this out in the 
 original mail. IOW, i'm not stupid; i'm well aware that 100 comes after 
 99 and that it contains 3 digits, not 2, thanks very much.

Nope, 100 comes *before* 99 when lexically sorting.

You must be new around here.

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



Re: [PHP] counting with leading zeros

2007-09-26 Thread brian

Robert Cummings wrote:

On Wed, 2007-09-26 at 19:10 -0400, brian wrote:


If my response to that gave you the impression i was
complaining, i assure you that i wasn't. I was simply suggesting
that i was wondering if there was a *much* simpler way to do this, ie.
without using several functions to process the filename. Yes, your
version *does* have some improvements. I'm not disagreeing with you. 
Perhaps i could have been more forthright in my praise for said 
improvements (i'm even going to use some of them--how about that?!)



Well, I'm not looking for praise... but asking a concise question goes a
long way towards meeting your desires.


How was my question *not* concise? If anything, it was my first reply to 
you that i could have spent more time on. Once again: your reply was 
valid, your corrections were taken into consideration, *but* i was 
simply wondering if there was a more (yes) concise way to do what i 
wanted. My humble apologies (really).


IOW, i'm not stupid; i'm well aware that 100 comes after 
99 and that it contains 3 digits, not 2, thanks very much.



Nope, 100 comes *before* 99 when lexically sorting.

You must be new around here.



Did i say anything there about lexically sorting? The query i posted 
here was about counting. That last comment? Yep, counting again. Now 
you're obviously just trying to be a dickhead. Well done.


You must be charming to work with.

brian

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



Re: [PHP] counting with leading zeros

2007-09-26 Thread Robert Cummings
On Wed, 2007-09-26 at 22:39 -0400, brian wrote:
 Robert Cummings wrote:
  On Wed, 2007-09-26 at 19:10 -0400, brian wrote:
  
 If my response to that gave you the impression i was
 complaining, i assure you that i wasn't. I was simply suggesting
 that i was wondering if there was a *much* simpler way to do this, ie.
 without using several functions to process the filename. Yes, your
 version *does* have some improvements. I'm not disagreeing with you. 
 Perhaps i could have been more forthright in my praise for said 
 improvements (i'm even going to use some of them--how about that?!)
  
  
  Well, I'm not looking for praise... but asking a concise question goes a
  long way towards meeting your desires.
 
 How was my question *not* concise? If anything, it was my first reply to 
 you that i could have spent more time on. Once again: your reply was 
 valid, your corrections were taken into consideration, *but* i was 
 simply wondering if there was a more (yes) concise way to do what i 
 wanted. My humble apologies (really).
 
 IOW, i'm not stupid; i'm well aware that 100 comes after 
 99 and that it contains 3 digits, not 2, thanks very much.
  
  
  Nope, 100 comes *before* 99 when lexically sorting.
  
  You must be new around here.
  
 
 Did i say anything there about lexically sorting? The query i posted 
 here was about counting. That last comment? Yep, counting again. Now 
 you're obviously just trying to be a dickhead. Well done.

When you use sort() the default behaviour is a lexical sort. This is why
the 100th index breaks your system. I'm not trying to be a dickhead,
just pointing out the flaw in your logic. You may be well aware that in
counting 100 comes after 99, but it would seem you are not well aware
that in a lexical sort 100 will come first.

 You must be charming to work with.

Quite.

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



Re: [PHP] counting with leading zeros

2007-09-26 Thread brian

Robert Cummings wrote:

When you use sort() the default behaviour is a lexical sort. This is why
the 100th index breaks your system. I'm not trying to be a dickhead,
just pointing out the flaw in your logic. You may be well aware that in
counting 100 comes after 99, but it would seem you are not well aware
that in a lexical sort 100 will come first.



I realise that this has been going on for some time now but i did say 
that this app will not--cannot--reach to 100. If it were to do so, my 
system would have become broken *long* before it reached sort(). 
Lexical sorting of '99'  '100' has nothing to do with this.


Just pointing out the flaw in your persistence with this.

b

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



Re: [PHP] counting with leading zeros

2007-09-26 Thread Robert Cummings
On Thu, 2007-09-27 at 00:23 -0400, brian wrote:
 Robert Cummings wrote:
  When you use sort() the default behaviour is a lexical sort. This is why
  the 100th index breaks your system. I'm not trying to be a dickhead,
  just pointing out the flaw in your logic. You may be well aware that in
  counting 100 comes after 99, but it would seem you are not well aware
  that in a lexical sort 100 will come first.
  
 
 I realise that this has been going on for some time now but i did say 
 that this app will not--cannot--reach to 100. If it were to do so, my 
 system would have become broken *long* before it reached sort(). 
 Lexical sorting of '99'  '100' has nothing to do with this.
 
 Just pointing out the flaw in your persistence with this.

Actually your original message said the following:

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.

Almost certainly last time I checked does not equal will not.

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



Re: [PHP] counting with leading zeros

2007-09-26 Thread Jim Lucas

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.


brian




Well, I didn't want to get into the middle of this cat fight, but now 
that the talks have completely gotten off topic.  I will interject...


Here is a function that is a bit simpler then either of yours.

Now, if you plan to delete any images in the list, this will not work.

But if you don't expect to delete any images in the list and always want 
the ($total_images + 1), then follow along.


?php

function getNextImage($path, $prefix) {
  $filelist = glob(${path}${prefix}*) or return '01';
  return str_pad(((int)count($filelist)+1), 2, '0', STR_PAD_LEFT);
}

?

This should work, I don't have a setup to test it on, but you should get 
the idea...


...

now remember...

if glob fails, it will return '01'
if glob returns and empty array, it will return '01'
if glob succeeds, it will return the array length + 1 padded (if needed) 
 by one zero


You asked for less function calls in your function, here you go

Jim

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