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

Dear Friends, I've got problems with a perl script in which I've got an if-else instruction. The perl program called readit.pl is used for a bilingual form: Catalan-Spanish. The first variable to be stored in ANUNCIS.TXT is the date. In Catalan, all week days start by DI and something else. So, to recognise Catalan, I use the week days. Here is my perl script for you to check if the if-else structure is correct:
my $n=1; open (LOG, "log/anuncis.txt") || die "$!"; while (<LOG>) { chomp; my @attributes = split /\|/; if ($attributes[0] =~m/Di*/){ print "<TABLE WIDTH=65% BORDER=2 ALIGN=CENTER>\n"; print "<TR BGCOLOR=#00FF00B><B>Anunci número $n del $attri +butes[0]</B></TR>\n"; print "<TR>De <B>$attributes[1] $attributes[2]</B> que vol + vendre <FONT COLOR=#FA8072><B>$attributes[6] a $attributes[7] ?.</B> +</FONT></TR>\n"; print "<TR>Us hi podeu posar en contacte al(s) telèfon(s) +<B><FONT COLOR=#FA8072>$attributes[8] // $attributes[9]</B></FONT></T +R>\n"; print "<TR>Segons l'horari que ens indica: <B><FONT COLOR= +#FA8072>$attributes[5]</B></FONT></TR>\n"; print "<TR>O bé al correu electrònic: <B>$attributes[10]</ +B> o a la seva URL <B>$attributes[11]</B></TR>\n"; print "<TR BGCOLOR=#FFA500><B>AQUESTS SÓN ELS SEUS COMENTA +RIS:</B>\n"; print "<BR><FONT COLOR=#8B0000><I><B>$attributes[12]</I></ +B></TR></FONT></TABLE>\n"; print "<BR><BR>\n";} else{ print "<TABLE WIDTH=65% BORDER=2 ALIGN=CENTER>\n"; print "<TR BGCOLOR=#9999FF><B>Anuncio número $n del $attri +butes[0]</B></TR>\n"; print "<TR>De <B>$attributes[1] $attributes[2]</B> que des +ea vender <FONT COLOR=#003366><B>$attributes[6] a $attributes[7] ?.</ +B></FONT></TR>\n"; print "<TR>Os podéis poner en contacto con el/ella en los +teléfonos <B><FONT COLOR=#003366>$attributes[8] // $attributes[9]</B> +</FONT></TR>\n"; print "<TR>Según el horario que nos idica: <B><FONT COLOR= +#003366>$attributes[5]</B></FONT></TR>\n"; print "<TR>Su e-Mail es: <B>$attributes[10]</B> y su URL: +<B>$attributes[11]</B></TR>\n"; print "<TR BGCOLOR=#FF9999><B>ESTOS SON SUS COMENTARIOS:</ +B>\n"; print "<BR><FONT COLOR=#333333><I><B>$attributes[12]</I></ +B></TR></FONT></TABLE>\n"; print "<BR><BR>\n";} $n++; } close LOG;
Thank you very much for your kind help!

Edit kudra, 2002-06-15 READMORE tag added

Replies are listed 'Best First'.
Re: If-else problem
by dree (Monsignor) on Jun 15, 2002 at 09:22 UTC
    Writing this is enough:
    if ($attributes[0] =~m/^DI/i){
    See perldoc perlref:
    i Do case-insensitive pattern matching. ^ Match the beginning of the line
Re: If-else problem
by r0b (Pilgrim) on Jun 15, 2002 at 14:06 UTC
    Just a tip: To make your code mode readable and less error prone try investigating the CGI Module to output HTML instead of doing it manually.

    ~~rob
    ____________________________________________________________
    eval pack "h*", "072796e647022245d445f475454494c5e622b3";

Re: If-else problem
by BigJoe (Curate) on Jun 15, 2002 at 09:28 UTC
    A quick look at 5:00 am (where I am from) I would make a change in you if statement from:
    if ($attributes[0] =~m/Di*/){
    To:
    if ($attributes[0] =~m/Di*/i){
    for case insensitvity.
    one thing you can do to make it read a little clearer is to try this:
    print <<EODUMP; <TABLE WIDTH=65% BORDER=2 ALIGN=CENTER> <TR BGCOLOR=#9999FF><B>Anuncio número $n del $attributes[0]</B></T +R> <TR>De <B>$attributes[1] $attributes[2]</B> que desea vender <FONT + COLOR=#003366><B>$attributes[6] a $attributes[7] €.</B></FONT></TR> <TR>Os podéis poner en contacto con el/ella en los teléfonos <B><F +ONT COLOR=#003366>$attributes[8] // $attributes[9]</B></FONT></TR> <TR>Según el horario que nos idica: <B><FONT COLOR=#003366>$attrib +utes[5]</B></FONT></TR> <TR>Su e-Mail es: <B>$attributes[10]</B> y su URL: <B>$attributes[ +11]</B></TR> <TR BGCOLOR=#FF9999><B>ESTOS SON SUS COMENTARIOS:</B> <BR><FONT COLOR=#333333><I><B>$attributes[12]</I></B></TR></FON +T> </TABLE> <BR><BR> EODUMP
    By printing like this you can format your HTML a little more sanely.

    --BigJoe

    Learn patience, you must.
    Young PerlMonk, craves Not these things.
    Use the source Luke.