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

Hi all I am pretty new on perl, I started some perl snippets yesterday. Here's the problem: I wrote the code below to extract all information from a text file named "dealerlist.txt",then output on a HTML table.
#!/usr/bin/perl print "content-type: text/html\n\n "; print "<head><title> DEEEalers </title></head> "; print "<body>\n "; print "<h1> Dealers Listing</h1> "; print "<table border>\n "; print "<tr><th>surname</th> "; print "<th>given</th>" ; print "<th>suburb</th>" ; print "<th>postcode</th></tr> "; open (names,"/var/www/cgi-bin/data/dealers.txt "); while ($aline = <names>) { chomp($aline); @fields = split(/,/,$aline); $surn = @fields[0]; $given = @fields[1]; $suburb = @fields[2]; $pcode = @fields[3]; print "<tr>"; print "<td>$surn </td>"; print "<td>$given </td>"; print "<td>$suburb </td>"; print "<td>$pcode </td>"; print "</tr>" ; } print "</table>\n "; print "</body>\n "; exit;
And this is what the file looks like:
PACKHAM,Staeven,MELBOURNE,3011, JACKSON,Shane,MELBOURNE,3004, PADDLE,Ben,MOONEE PONDS,3053,C RAHIM,Stuart,MELBOURNE,3000,C LAI,Bernard,MELBOURNE,3039, BAIRD,Michael,MELBOURNE,2073, FAIRSERVICE,Daniel,MELBOURNE,3000,N GAKOVIC,D,MELBOURNE,3004, MALCOLM,Raymond,MASCOT,2060,X HALEY,Peter,MIRANDA,2640, WALKER,Richard,MALVERN,3150,N HALL,David,MELBOURNE,3002, VALLAK,Simon,MELBOURNE,3043, BALLANTYNE,Anne,MELBOURNE,3000,C BALLER,Sarah,MELBOURNE,3000,C
Now I want to extract only the record of whose surname begins with "B", how can I modify my original code to realize this function?

Replies are listed 'Best First'.
Re: Help on String manipulation
by davido (Cardinal) on May 13, 2006 at 05:04 UTC

    Rework your while(){} loop like this:

    while ($aline = <names>) { chomp($aline); @fields = split(/,/,$aline); $surn = @fields[0]; next unless $surn =~ m/^B/i; # Skip entries that don't start # with B or b. $given = @fields[1]; $suburb = @fields[2]; $pcode = @fields[3]; print "<tr>"; print "<td>$surn</td>"; print "<td>$given</td>"; print "<td>$suburb</td>"; print "<td>$pcode</td>"; print "</tr>\n"; }

    Dave

      thank you so much dave.
Re: Help on String manipulation
by Zaxo (Archbishop) on May 13, 2006 at 05:07 UTC

    You would, in line 27, say,

    next if $line[0] !~ /^B/;
    You can certainly do better with some practice and a better design.

    The first step is to place data management above writing html. That is a matter of reporting, and should be well-removed from data processing.

    After Compline,
    Zaxo

      Actually I am just a student, and this is a task assigned to me from my tutor. Since he didn't give us any materials on learning perl, I have to learn all fundamentals from web by myself. I know the algorithm of this snippet is awful easy but I was just stuck on it b'cause I know little about Perl. Anyway, thank you guys for help,really appreciate!

        So I just did your homework for you? I feel so used. lol.

        Do take fifteen minutes to read through perlsyn, and then through perlretut. At least then you'll have the background knowlege that your tutor intended for you to learn, but that we deprived you of by answering your question rather than teaching.


        Dave

Re: Help on String manipulation
by Samy_rio (Vicar) on May 13, 2006 at 05:18 UTC

    Hi jiaoziren, Try this,

    TIMTOWDI

    use strict; use warnings; use HTML::TableTiler; my @matrix; while(<DATA>){ chomp; my @row = split/,\s*/, $_; next if ($row[0] !~ m/^B/); push(@matrix, [@row]); } my $tt = HTML::TableTiler->new(); print $tt->tile_table(\@matrix); __DATA__ PACKHAM,Staeven,MELBOURNE,3011, JACKSON,Shane,MELBOURNE,3004, PADDLE,Ben,MOONEE PONDS,3053,C RAHIM,Stuart,MELBOURNE,3000,C LAI,Bernard,MELBOURNE,3039, BAIRD,Michael,MELBOURNE,2073, FAIRSERVICE,Daniel,MELBOURNE,3000,N GAKOVIC,D,MELBOURNE,3004, MALCOLM,Raymond,MASCOT,2060,X HALEY,Peter,MIRANDA,2640, WALKER,Richard,MALVERN,3150,N HALL,David,MELBOURNE,3002, VALLAK,Simon,MELBOURNE,3043, BALLANTYNE,Anne,MELBOURNE,3000,C BALLER,Sarah,MELBOURNE,3000,C

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

      cheers mate.I'll keep your snippet and do some more studies on it.
Re: Help on String manipulation
by TedPride (Priest) on May 13, 2006 at 20:26 UTC
    You made a number of mistakes in your HTML. For instance, you say the type is text/html, yet you don't have an html tag at all. You also don't say what size the border is supposed to be. And you don't define a background color (some browsers default to grey, which isn't pretty). Maybe you want something more like:
    print <<OUT; Content-Type: text/html <html> <head> <title> DEEEalers </title> </head> <body bgcolor="#FFFFFF"> <h1> Dealers Listing</h1> <table border="1"> <tr><th>surname</th><th>given</th><th>suburb</th><th>postcode</th></tr +> OUT open (NAMES, "/var/www/cgi-bin/data/dealers.txt"); while (<NAMES>) { next if !m/^B/; chomp; print "<tr><td>", join ("</td><td>", (split /[,\s]+/, $_)[0..3]), +"</td></tr>\n"; } close (NAMES); print <<OUT; </table> </body> </html> OUT
    I suggest figuring out what all of this does and rewriting it in your own format, rather than copying it verbatim. Your teacher might check here, who knows.