At 12:37 PM +0100 3/24/00, Frédéric Guillaume wrote: >I'm trying to make a loop to print the list while reading the >template. At that point i have a big problem because the loop >prints 'n' times the same subject (the first in the list), and 'n' >is the total number of subjects. > >Here is the code: > >##open the index and swallow it > > open (INDEX, "<path:subjects.txt") or die "$!\n"; > $nbSuj = <INDEX>; > @index = <INDEX>; > close (INDEX); > > @rindex = reverse @index; > >##open the template and print it to STDOUT > > open (TEMPL, "<path:forum.tmp"); > while (<TEMPL>){ > if ($. == 1 .. 38){ > $_ =~ s/%nbSuj%/$nbSuj/; > print $_; > >##the loop: > }elsif ($. == 40){ > foreach $line(@rindex){ > ($ID, $Sujet, $nbRep, $Auteur, $date) = >split(/,/, $line); > $_=~ s/%ID%/$ID/; > $_ =~ s/%Sujet%/$Sujet/; > $_ =~ s/%Auteur%/$Auteur/; > $_ =~ s/%date%/$date/; > $_ =~ s/%nbRep%/$nbRep/; > print $_; > } > }else{ print $_;} > } > close (TEMPL); > >the problem is that for each $line it does not reinitialize the >variables in $_ (hope i'm clear enough... but my english is so bad >:.(( Looking at the code, I think what is happening is that the first time through the foreach loop, the various %ID%, %Sujet%, etc. placeholders are replaced with the first set of values from your first $line. For each subsequent pass, there are no placeholders, nothing gets replaced by s///, and the unchanged line prints again. I don't want to create all the files, etc, to test this, but the modified foreach loop below might work: foreach $line (@rindex) { ($ID, $Sujet, $nbRep, $Auteur, $date) = split(/,/, $line); $tmp = $_; $tmp =~ s/%ID%/$ID/; $tmp =~ s/%Sujet%/$Sujet/; $tmp =~ s/%Auteur%/$Auteur/; $tmp =~ s/%date%/$date/; $tmp =~ s/%nbRep%/$nbRep/; print $tmp; } David Steffen, Ph.D. President, Biomedical Computing, Inc. <http://www.biomedcomp.com/> Phone: (713) 610-9770 FAX: (713) 610-9769 E-mail: steffen@biomedcomp.com ==== Want to unsubscribe from this list? ==== Send mail with body "unsubscribe" to macperl-webcgi-request@macperl.org