Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Store last value of for loop

by sandy1028 (Sexton)
on Jan 15, 2009 at 05:15 UTC ( [id://736459]=perlquestion: print w/replies, xml ) Need Help??

sandy1028 has asked for the wisdom of the Perl Monks concerning the following question:

I have an articles where each article contains 2 or more articles nested. I have used threading concept to thread it. The values of the article is not in sequential. Suppose first article has 2 articles within it and second article has 6 articles. The value of the article is Count of articles_id displays 2,6,etc. $i=0; for my $id(@article_id){ print $i; $i++; } output will be 1,2,1,2,3,4,5,6 The for loop initializes the value as 0 for each article. How to store the value of last value of the varible when exits from each iteration. So that the value is 1,2,3,4,5,6,7,8.

Replies are listed 'Best First'.
Re: Store last value of for loop
by tilly (Archbishop) on Jan 15, 2009 at 05:34 UTC
    It is actually potentially more difficult than that. If you're actually using threads, then one iteration should start before the other finishes.

    What you need to do is use threads::shared then share $i across threads. That way no matter what thread updates it, the others will see the update. With one caveat. There might be a possible race condition where 2 threads try to ++ at the same time and you create a problem internally. This should be rare, but to be safe you should lock. So that looks something like this:

    use strict; use threads; use threads::shared; my $i :shared; $i = 1; # Time and code passes. for my $id (@article_id) { lock($i); print "$i\n"; $i++; } # More code.
    Note that if you want to put long-running code in that loop you will lose the benefits of threading because different threads will block on the lock on $i. If you want to solve that you can do this:
    use strict; use threads; use threads::shared; { my $i :shared; sub get_i { lock($i); return ++$i; } } # Time and code passes. for my $id (@article_id) { my $i = get_i(); print "$i\n"; # Do lengthy processing. } # More code.

    Update: johngg pointed out that the modules were named threads but my code called them thread instead. Fixed.

Re: Store last value of for loop
by jethro (Monsignor) on Jan 15, 2009 at 05:51 UTC

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://736459]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-24 18:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found