Click here to Skip to main content
13,667,677 members
Rate this:
Please Sign up or sign in to vote.
See more:
for ($i=0;$i<1000;$i++)
my $nr1= 10 + int(rand(0x150));
my $nr2 = 10 + 1 after 100 loops 

So i have this script above in a loop of 1000,
is it possible to add 1 after every 100 loops for my $nr2 ???
$nr1 adds every count plus 1.

All help appreciated 

Posted 10-Aug-14 5:54am
Updated 10-Aug-14 5:55am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Inside your 'for' loop:
if (i % 100 == 0) {
  $nr += 1;
dennis2704 10-Aug-14 12:47pm
I cannot get it to work it doesnt add a number to it??
what am i doin wrong :(

With just copying the code inside my for loop it does not count??
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

How about this:
$nr2++ unless $i%100;

dennis2704 10-Aug-14 13:13pm
i really appreciate your help and sollution but i can not get it working could you show me with the script from my question how it complete needs to look like.

Sorry for being rettarded but i really can get an increase of the calculation :(
Andreas Gieriet 10-Aug-14 13:41pm
my $nr2 = 0;
my $i = 0;
for ($i = 0; $i < 100; $i++)
$nr2++ unless $i % 100;
dennis2704 10-Aug-14 14:02pm
yes ok that is working but i want the whol code inside another loop and that loop is set to 1000 so that every 100 loops its adding 1 to the $nr2.
Andreas Gieriet 11-Aug-14 2:07am
This is something else than asked in the initial question. Show your current code with that nested loop and we can figure out what is wrong.
dennis2704 11-Aug-14 7:09am
Ok sorry for the miscommunication then from the beginning of my question:
for ($i=0;$i<1000;$i++)
my $nr1 = 10 + int(rand(9));
my $nr2 = 10 + 1 added each time it hits the 100 loop;
print "$nr1\n";
print "$nr2\n";
sleep 5;
here is my script basicly what im tryin to get is for the $nr2 to have an automaticly counting each time the script runs through the 100 loop there is 1 added every 100 times loop
So for my script it should be at 110 at the end of the loop. I hope you understand what i mean. Maybe if its possible could i also use a maximum number for example if i would put the loop on infinite that the maximum of increasement for the sumn starts again from 0 after the 100 is maximum for the counting??
Andreas Gieriet 11-Aug-14 11:36am
If you loop 1000 times and only incremet $nr2 every 100st loop, then you get 10.
Your question is not clear.
What is clear is that you have no understanding of my. As you wrote your code, the $nr1 and $nr2 are local to the loop block and therefore get initialized with each iteration and stay the same. You must move out of the loop the my $nr1 and my $nr2 and delete the my inside the loop block. See my initial comment what I mean by this. I wonder why you did not follow this ins your code above and simply copied the original code again... This is not helpful at all.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web05-2016 | 2.8.180820.1 | Last Updated 10 Aug 2014
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100