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{$_} ? $_= " " : $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
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 # 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{$_} ? $_= " " : $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 $_ = " "; } 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
In reply to Re: printing a table in html using data read from a text file
by tachyon
in thread printing a table in html using data read from a text file
by videogamer06
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |