in reply to printing a table in html using data read from a text file

What you seem to be asking for is "how do I avoid printing duplicate fields". The answer to this is by using a hash. A hash can not have identical keys so as we iterate over the fields we check for its prior existence via a hash key. If the key already exists the field is a duplicate, if not it is unique. If unique we add it to our hash keys so next time it will be recognised as a duplicate.

This will dump a table like you want....

tachyon

my %dup; # duplicate charachters hash print "<TABLE BORDER='1'>\n"; for (<DATA>) { chomp; next if /^\s*$/; my @elements = split", ",$_; map{exists $dup{$_} ? $_= "&nbsp;" : $dup{$_}++ }@elements; print_row(@elements); } print "</TABLE>\n"; sub print_row { print"<TR>"; print"<TD>$_</TD>" for @_; print"</TR>\n"; } __DATA__ csc, tech, base csc, comp, acm csc, mous, base

Update

Here is the code in plain perl form. I have added this because it was privately suggested that my initial post was totally lacking in explanation and too complicated. Too much perl lately I guess...

# start by printing the opening table token print "<TABLE BORDER='1'>\n"; # now lets iterate over the lines of data foreach my $line(<DATA>) { # chomp of the \n from each line chomp($line); # ignore blank lines in the data next if $line =~ m/^\s*$/; # split the line into an array of elements my @elements = split", ",$line; # now iterate over the elements, if the element has # already been seen then set it to &nbsp; # which is the HTML token for a space, otherwise # add the element to our duplicates hash # I did this using map as shown: # map{exists $dup{$_} ? $_= "&nbsp;" : $dup{$_}++ }@elements; # in this context map is just a fancy way of # writing a loop structure. foreach (@elements) { # the magical $_ is assigned to each element # of our array @elements # the (condition) ? do this : (else) do that # construct is a one line version of the # if (cond) {do this} else {do that} below # so the map has the effect of seven lines... if (exists $dup{$_}) { # $_ is magical -> by changing $_ # we change this element of @elements $_ = "&nbsp;"; } else { # define a new key of our %dup hash # you can just do $dup{$_}++ to assign # a value of one to $dup{$_} but this # is more obvious $dup{$_} = "seen, so define $_ key"; } } # print out the line via our sub, formating # each element for a HTML table print_row(@elements); } print "</TABLE>\n"; sub print_row { print"<TR>"; # @elements is passed to sub via @_ array, this # print"<TD>$_</TD>" for @_; # is just short form for: foreach my $element(@_) { print"<TD>$element</TD>" } print"</TR>\n"; } __DATA__ csc, tech, base csc, comp, acm csc, mous, base

Replies are listed 'Best First'.
Re: Re: printing a table in html using data read from a text file
by petdance (Parson) on Jun 08, 2001 at 17:33 UTC
    sub print_row { print"<TR>"; print"<TD>$_</TD>" for @_; print"</TR>\n"; }
    For stuff like this, I so love using the stuff in CGI.pm. It's so slick to be able to say
    print TR( td( \@_ ) );
    Or, if I want to build a table at once, do:
    my @rows; for ( blah blah ) { ... push( @rows, TR( td( \@_ ) ); } print table( @rows );

    xoxo,
    Andy

    %_=split/;/,".;;n;u;e;ot;t;her;c; ".   #   Andy Lester
    'Perl ;@; a;a;j;m;er;y;t;p;n;d;s;o;'.  #   http://petdance.com
    "hack";print map delete$_{$_},split//,q<   andy@petdance.com   >