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

Dear Friends, I'm having a new problem with the following script:
#!/usr/bin/perl -wT #author: Kunt Unt #Creation date: 10/06/2002 use strict; my $n=1; open (LOG, "anuncis.txt") || die "$!"; while (<LOG>) { chomp; my @attributes = split /\|/; foreach my $attribute (@attributes) { print "<TABLE WIDTH=65% BORDER=1 ALIGN=CENTER>\n"; print "<TR BGCOLOR=#00FF00B>Anunci número $n del $attribut +es[0]</TR>\n"; print "<TR>De $attributes[1] $attributes[2] que vol vendre + $attributes[6] a $attributes[7] €.</TR>\n"; print "<TR>Us hi podeu posar en contacte al(s) telèfon(s) +$attributes[8] // $attributes[9]</TR>\n"; print "<TR>Segons l'horari que ens indica: $attributes[5]< +/TR>\n"; print "<TR>O bé al correu electrònic $attributes[10] o a l +a seva URL $attributes[11]</TR>\n"; print "<TR>Aquests són els seus comentaris:\n"; print "<P> <FONT COLOR=#DAA520>$attributes[12]</TR></TABLE +>\n"; print "<BR><BR>\n"; } $n++; } close LOG; print qq(<HR>Retorn a la nostra pàgina <a href="http://localhost/anunc +is.htm"> principal</a>);
It's supposed to print only the atrributes in each line, which it does, but it shows me as many messages as attributes the user has input. How can I solve it? Any idea? Thank you very much in advance

Replies are listed 'Best First'.
Re: Too many outputs
by derby (Abbot) on Jun 12, 2002 at 19:02 UTC
    Thats because you're looping for each attribute!

    ... foreach my $attribute (@attributes) { ...

    remove the attribute loop:

    /usr/bin/perl -wT #author: Kunt Unt #Creation date: 10/06/2002 use strict; my $n=1; open (LOG, "anuncis.txt") || die "$!"; while (<LOG>) { chomp; my @attributes = split /\|/; print "<TABLE WIDTH=65% BORDER=1 ALIGN=CENTER>\n"; print "<TR BGCOLOR=#00FF00B>Anunci número $n del $attributes[0]</TR +>\n"; print "<TR>De $attributes[1] $attributes[2] que vol vendres $attrib +utes[6] a $attributes[7] €.</TR>\n"; print "<TR>Us hi podeu posar en contacte al(s) telèfon(s) $attribut +es[8] // $attributes[9]</TR>\n"; print "<TR>Segons l'horari que ens indica: $attributes[5]</TR>\n"; print "<TR>O bé al correu electrònic $attributes[10] o a la seva UR +L $attributes[11]</TR>\n"; print "<TR>Aquests són els seus comentaris:\n"; print "<P> <FONT COLOR=#DAA520>$attributes[12]</TR></TABLE>\n"; print "<BR><BR>\n"; $n++; } close LOG; print qq(<HR>Retorn a la nostra pàgina <a href="http://localhost/anunc +is.htm"> principal</a>);

    -derby

      Thank you so much! It already works!!!! You may see it working at http://www.sipocasio.com/catala.htm, click on "anuncis". This will be operative tomorrow!!!!
Re: Too many outputs
by George_Sherston (Vicar) on Jun 12, 2002 at 19:04 UTC
    The problem is you have a foreach loop *inside* a while loop, so for each line of your log file, you're going through your @attributes array and printing the whole lot out $#attributes times.

    I think it would work if you just took out the line starting foreach and the corresponding } at the end, though I haven't tried this out.

    § George Sherston