in reply to Store last value of for loop

The for loop you are showing does not initialize the value as 0 and will print 0, not 1, as the first value:

perl -e ' my @a=(1,1,1,1,1,1,1,1); $i=0; for my $id(@a){ print $i; $i+ ++; } ' outputs 01234567

But $i=0; certainly does initialize. Put that $i somewhere it is executed only once.

I hope your code was only an example. The number of articles in @article_id can be found simply by providing scalar context:

perl -e ' my @a=(1,1,1,1,1,1,1,1); my $i= @a; print "$i\n"; ' outputs 8

Replies are listed 'Best First'.
Re^2: Store last value of for loop
by sandy1028 (Sexton) on Jan 15, 2009 at 09:47 UTC
    There are n number of articles and each articles contains m number of files. How to use threading concept and print the values sequentially. Using of for loops on each 'n' number of articles it prints value from 1 to m files. second articles initializes again to 1 and print m files. The problem is n articles and m number of files within n articles should print in sequential order.

      If you want to print something sequential it makes no sense using threads for that particular problem. You are very vague and abstract about your problem. Why do you need the threads? How big can n and m get?

      If you need threads for some other reason then you might store the output from each thread in a different variable (if it fits in memory) and combine them later, or store the output in a different file per thread.

      Or use only one file with the number of the thread prepended on each line. Then simply sort the file and print. But for this you need file locking or some other signaling mechanism so that two threads don't write to the file at the same time. If you have a database engine, the same can be done without locking problems by using the thread number as key