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

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.