Re: [PHP] Loop within Loop help please
On Tue, 28 Sep 2004 16:22:18 -0300, Eduardo Sampaio <[EMAIL PROTECTED]> wrote: > You missed the $count = 0; > right below it Andrew... his code works just like the modulus one... Whoops, my mistake. I write code, really I do. :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Loop within Loop help please
Well, elegance, is in the eye of the beholder !!.. but ya, i certainly wasnt contesting the modulas solutiion, ive used mod in similar cases myself, i was just adding some variety. Jason "Jay Blanchard" <[EMAIL PROTECTED]> wrote: > > [snip] > well, actually, the code resets the count var, so it makes absolutely no > difference how many iterations the loop makes, it could iterated > indefinately and still function identically. > [/snip] > > That is quite true, but which is more elegant? > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loop within Loop help please
Jay Blanchard wrote: [snip] well, actually, the code resets the count var, so it makes absolutely no difference how many iterations the loop makes, it could iterated indefinately and still function identically. [/snip] That is quite true, but which is more elegant? Whichever way Martha Stewart would write it. ;) -- John C. Nichel ÜberGeek KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Loop within Loop help please
[snip] well, actually, the code resets the count var, so it makes absolutely no difference how many iterations the loop makes, it could iterated indefinately and still function identically. [/snip] That is quite true, but which is more elegant? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loop within Loop help please
Hey, im sorry, i didnt read the other responses to your post, ive posted the same thing... Jason "Jason Davidson" <[EMAIL PROTECTED]> wrote: > > well, actually, the code resets the count var, so it makes absolutely no > difference how many iterations the loop makes, it could iterated > indefinately and still function identically. > > Jason > > Andrew Kreps <[EMAIL PROTECTED]> wrote: > > > > On Tue, 28 Sep 2004 10:45:19 -0700, Jason Davidson <[EMAIL PROTECTED]> > wrote: > > > I see lots of references to modulus, which works fantastic.. but how > > > abut just testing if its equal to 20, then there isnt that extra > > > arthmitc call.. this is just another way to do the same thing.. take > > > your pick.. unless someone can see a bad reason for this method. > > > > > > $count = 0; > > > foreach . { > > >if($count == 20) { > > > . > > > > The only thing wrong with this method is that it doesn't fit the > > problem's specifications.The original poster wanted a way to > > trigger the code every 20 iterations of an unknown sized loop > > (implying that the loop would have 40 or more items), whereas the > > solution above only triggers it on iteration 20. Other than that, > > it's a perfectly wonderful piece of code. > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loop within Loop help please
well, actually, the code resets the count var, so it makes absolutely no difference how many iterations the loop makes, it could iterated indefinately and still function identically. Jason Andrew Kreps <[EMAIL PROTECTED]> wrote: > > On Tue, 28 Sep 2004 10:45:19 -0700, Jason Davidson <[EMAIL PROTECTED]> wrote: > > I see lots of references to modulus, which works fantastic.. but how > > abut just testing if its equal to 20, then there isnt that extra > > arthmitc call.. this is just another way to do the same thing.. take > > your pick.. unless someone can see a bad reason for this method. > > > > $count = 0; > > foreach . { > >if($count == 20) { > > . > > The only thing wrong with this method is that it doesn't fit the > problem's specifications.The original poster wanted a way to > trigger the code every 20 iterations of an unknown sized loop > (implying that the loop would have 40 or more items), whereas the > solution above only triggers it on iteration 20. Other than that, > it's a perfectly wonderful piece of code. > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loop within Loop help please
You missed the $count = 0; right below it Andrew... his code works just like the modulus one... On Tue, 28 Sep 2004 12:10:21 -0700, Andrew Kreps <[EMAIL PROTECTED]> wrote: > On Tue, 28 Sep 2004 10:45:19 -0700, Jason Davidson <[EMAIL PROTECTED]> wrote: > > I see lots of references to modulus, which works fantastic.. but how > > abut just testing if its equal to 20, then there isnt that extra > > arthmitc call.. this is just another way to do the same thing.. take > > your pick.. unless someone can see a bad reason for this method. > > > > $count = 0; > > foreach . { > >if($count == 20) { > > . > > The only thing wrong with this method is that it doesn't fit the > problem's specifications.The original poster wanted a way to > trigger the code every 20 iterations of an unknown sized loop > (implying that the loop would have 40 or more items), whereas the > solution above only triggers it on iteration 20. Other than that, > it's a perfectly wonderful piece of code. > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loop within Loop help please
Andrew Kreps wrote: On Tue, 28 Sep 2004 10:45:19 -0700, Jason Davidson <[EMAIL PROTECTED]> wrote: I see lots of references to modulus, which works fantastic.. but how abut just testing if its equal to 20, then there isnt that extra arthmitc call.. this is just another way to do the same thing.. take your pick.. unless someone can see a bad reason for this method. $count = 0; foreach . { if($count == 20) { . The only thing wrong with this method is that it doesn't fit the problem's specifications.The original poster wanted a way to trigger the code every 20 iterations of an unknown sized loop (implying that the loop would have 40 or more items), whereas the solution above only triggers it on iteration 20. Other than that, it's a perfectly wonderful piece of code. You left this line out of Jason's post when you snipped it $count = 0; ie, everytime $count reached 20, run special code, and reset count to $20. -- John C. Nichel ÜberGeek KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loop within Loop help please
On Tue, 28 Sep 2004 10:45:19 -0700, Jason Davidson <[EMAIL PROTECTED]> wrote: > I see lots of references to modulus, which works fantastic.. but how > abut just testing if its equal to 20, then there isnt that extra > arthmitc call.. this is just another way to do the same thing.. take > your pick.. unless someone can see a bad reason for this method. > > $count = 0; > foreach . { >if($count == 20) { > . The only thing wrong with this method is that it doesn't fit the problem's specifications.The original poster wanted a way to trigger the code every 20 iterations of an unknown sized loop (implying that the loop would have 40 or more items), whereas the solution above only triggers it on iteration 20. Other than that, it's a perfectly wonderful piece of code. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loop within Loop help please
* and then Jason Davidson declared > I see lots of references to modulus, which works fantastic.. but how > abut just testing if its equal to 20, then there isnt that extra > arthmitc call.. this is just another way to do the same thing.. take > your pick.. unless someone can see a bad reason for this method. > > $count = 0; > foreach . { >if($count == 20) { > . > $count = 0; > } else { > $count++; > } > } That would be fine, but im afraid in my effort to make the example simple i forgot to ad a line: $count++; ;-) -- Nick W -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loop within Loop help please
* and then Jesse Castro declared > [snip] > $count=0; > foreach($someArray as $someVal) { > if($count is divisible by 20 exactly) { > // do some stuff > } > //do some stuff > } > ?> > [/snip] > > if($count is divisible by 20 exactly) > Can be written as > If ($count %20 == 0) > % is the modulus operator and works like the division > operator, only it returns the remainder instead of the > quotient. Ahhh.. thankyou very much jesse, that's great! -- Nick W -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loop within Loop help please
I see lots of references to modulus, which works fantastic.. but how abut just testing if its equal to 20, then there isnt that extra arthmitc call.. this is just another way to do the same thing.. take your pick.. unless someone can see a bad reason for this method. $count = 0; foreach . { if($count == 20) { . $count = 0; } else { $count++; } } Nick Wilson <[EMAIL PROTECTED]> wrote: > > hi everyone, > > I have a simplified bit of code below: >foreach($someArray as $someVal) { > //do some stuff > } > ?> > > What i'd like to do is have a count inside that loop that will trigger > some action every 20 iterations of the foreach. Like this: >$count=0; > foreach($someArray as $someVal) { > if($count is divisible by 20 exactly) { > // do some stuff > } > //do some stuff > } > ?> > > How might i do that? > > Much thanks... > -- > Nick W > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loop within Loop help please
Nick Wilson wrote: hi everyone, I have a simplified bit of code below: What i'd like to do is have a count inside that loop that will trigger some action every 20 iterations of the foreach. Like this: How might i do that? if($count % 20 == 0 ) { Janet -- Janet Valade -- janet.valade.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Loop within Loop help please
How might i do that? Something like that. Justin Much thanks... -- Nick W -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loop within Loop help please
* and then Marek Kilimajer declared > % operator: http://www.php.net/operators Thanks EVERYONE, i feel like i should have known that, but i didnt, my thanks! - and the guy that suggested the other way? that was how i was trying to do it ;-) -- Nick W -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loop within Loop help please
* and then Pablo Gosse declared > if ($count%20 == 0) > > http://ca3.php.net/manual/en/language.operators.arithmetic.php in the > manual (look for Modulus). Yes, thankyou very much, got a bunch of similar replys in my personal inbox to ;-) And there was me sifting through the math functions! hehe... -- Nick W -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Loop within Loop help please
[snip] What i'd like to do is have a count inside that loop that will trigger some action every 20 iterations of the foreach. Like this: How might i do that? [/snip] With modulus ...http://us2.php.net/manual/en/language.operators.arithmetic.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loop within Loop help please
% operator: http://www.php.net/operators Nick Wilson wrote: hi everyone, I have a simplified bit of code below: What i'd like to do is have a count inside that loop that will trigger some action every 20 iterations of the foreach. Like this: How might i do that? Much thanks... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Loop within Loop help please
[snip] I have a simplified bit of code below: What i'd like to do is have a count inside that loop that will trigger some action every 20 iterations of the foreach. Like this: [/snip] if ($count%20 == 0) http://ca3.php.net/manual/en/language.operators.arithmetic.php in the manual (look for Modulus). HTH. Pablo -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loop within Loop help please
Nick Wilson wrote: hi everyone, I have a simplified bit of code below: What i'd like to do is have a count inside that loop that will trigger some action every 20 iterations of the foreach. Like this: How might i do that? Much thanks... $count = 0; foreach ( $someArray as $someVal ) { if ( is_int ( $count / 20 ) ) { // do this } // do this $count++; } -- John C. Nichel ÜberGeek KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php